DataSource Controls :: Displaying Total Record With Count?
Dec 12, 2010
I have the following ...
<asp:SqlDataSource ID="totalcount" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT COUNT ([CID]) FROM [WWAMembers] WHERE([CID] IS NOT NULL)"></asp:SqlDataSource>
how do I get the totals displayed with label or other control?
I am counting from a table for that i have written code as below
protected void get_Id_Afterpoint() { int counter = 0; string strSql = "select count(*) as ID from tblEnergy where ID=?"; OdbcCommand com = new OdbcCommand(strSql, con); com.Parameters.AddWithValue("ID", DropDownList1.SelectedValue); OdbcDataAdapter oda = new OdbcDataAdapter(com); DataTable dt = new DataTable(); oda.Fill(dt); if (dt.Rows.Count == 0) { lblID2.Text = "1"; } else { counter = dt.Rows.Count; counter = counter + 1; lblID2.Text = Convert.ToString(counter); } }
there is no record related to DropDownList1.SelectedValue. but as i am counting if(dt.rows.count) and i put break point on the bolded part it shows 1 record. how it can be possible?
I HAVE A GRID VIEW IN asp.net + VB Code. There is a drop down list and the selected data is displayed in web page. I placed a label in web page and code behind i used following code.
Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound Label1.Text = GridView1.Rows.Count.ToString() Dim ONYO As Integer = 0 Dim DONE As Integer = 0 For i As Integer = 0 To GridView1.Rows.Count - 1 If GridView1.Rows(i).Cells(0).Text = "ON YO" Then
I wanted to get the total row count in a GridView's databound event handler, so I tried the following:
protected void grid_DataBound(object sender, EventArgs e) { GridView grid = (GridView)sender; DataSet data = grid.DataSource as DataSet; if (data == null) return; int totalRows = data.Tables[0].Rows.Count; }
The problem is that the grid.DataSource property is null. The DataSourceID property is not null, so does that mean that I can't access the DataSource object in the DataBound event handler unless I assign the DataSource property directly?
Edit1: Here is the code in my GridHelper class for adding rowcount to the BottomPagerRow. I would like to get rid of the requirement to pass in the ObjectDataSource, but can't because I need to use the Selected event to get total row count. Hence the reason for the question. I also think this might be better in a custom control where I can access ViewState, and/or create child controls during the Init event (I still have a problem to resolve with the way the pager renders with an extra cell), but then I'd still have the problem of how to get to the total row count when the DataSource itself doesn't appear to be available in any GridView events.
Edit 2: Not really relevant to the specific problem, but I resolved the problem I was having with the pager rendering so i've updated the code posted. I found the trick here: [URL]
#region Fields private int totalRows = -1; #endregion #region Constructors /// <summary> /// Initializes a new instance of the <see cref="GridHelper"/> class. /// Adds EventHandlers to the GridView to display results from the ObjectDataSource in the footer. /// Marked as obsolete because AddResultsToFooter method provides a static access to the same functionality /// An instance of GridHelper is required by the passed in GridView to store the totalRows value between the two event handlers /// </summary> /// <param name="grid">The grid.</param> /// <param name="source">The ObjectDataSource linked to the GridView.</param> [Obsolete("Use AddResultsToFooter instead.")] [EditorBrowsable(EditorBrowsableState.Never)] public GridHelper(GridView grid, ObjectDataSource source) { source.Selected += source_Selected; grid.PreRender += grid_PreRender; } #endregion #region Event Handlers private void grid_PreRender(object sender, EventArgs e) { GridView grid = (GridView)sender; if (grid.HeaderRow != null) grid.HeaderRow.TableSection = TableRowSection.TableHeader; //Add a cell to the bottom pager row to display the total results if (grid.BottomPagerRow == null || !grid.BottomPagerRow.Visible) return; //Get the control used to render the pager //http://michaelmerrell.com/2010/01/dynamically-modifying-the-asp-net-gridview-paging-control/ Table tblPager = grid.BottomPagerRow.Cells[0].Controls[0] as Table; if (tblPager == null) return; if (totalRows < 0) { //The DataSource has not been refreshed so get totalRows from round trip to client addResultsToPagerTable(tblPager, grid.Attributes["results"]); return; } int firstRow = grid.PageIndex * grid.PageSize + 1; int lastRow = firstRow + grid.Rows.Count - 1; string results; if (totalRows <= grid.PageSize) results = string.Format("<span class='grid-pager'>{0} Results</span>", totalRows); else results = string.Format("Results <b>{0}</b> to <b>{1}</b> of <b>{2}</b>", firstRow, lastRow, totalRows); addResultsToPagerTable(tblPager, results); //Need to store the information somewhere that is persisted via ViewState, and we don't have access to ViewState here grid.Attributes.Add("results", results); } /// <summary> /// Handles the Selected event of the source control. Gets the total rows, since it is not possible to access them from the GridView. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs"/> instance containing the event data.</param> private void source_Selected(object sender, ObjectDataSourceStatusEventArgs e) { if (e.ReturnValue is DataView) totalRows = ((DataView)e.ReturnValue).Count; else if (e.ReturnValue is EntitySet) totalRows = ((EntitySet)e.ReturnValue).Count; } #endregion #region Private Methods private static void addResultsToPagerTable(Table tblPager, string results) { //http://michaelmerrell.com/2010/01/dynamically-modifying-the-asp-net-gridview-paging-control/ //Get a handle to the original pager row TableRow pagesTableRow = tblPager.Rows[0]; //Add enough cells to make the pager row bigger than the label we're adding while (pagesTableRow.Cells.Count < 10) pagesTableRow.Cells.Add(new TableCell { BorderStyle = BorderStyle.None }); //Add a new cell in a new row to the table TableRow newTableRow = new TableRow(); newTableRow.Cells.AddAt(0, new TableCell { Text = results, BorderStyle = BorderStyle.None, ColumnSpan = pagesTableRow.Cells.Count }); tblPager.Rows.AddAt(0, newTableRow); } #endregion #region Public Methods /// <summary> /// Adds EventHandlers to the GridView to display results from the ObjectDataSource in the footer. /// </summary> /// <param name="grid">The GridView.</param> /// <param name="source">The ObjectDataSource linked to the GridView.</param> public static void AddResultsToFooter(GridView grid, ObjectDataSource source) { if (grid == null) throw new ArgumentNullException("grid", "grid is null."); if (source == null) throw new ArgumentNullException("source", "source is null."); new GridHelper(grid, source); } #endregion
Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Label3.Text = GridView1.Rows.Count.ToString() End Sub
I'll start off with a snapshot of the application i'm working on.
Basically, what's going on is i'm using a bunch of sqldatasources to track truck loads (shown) based off a date/ date range. As there are generally multiple loads that all have the same information, so the information is grabbed with a SELECT DISTINCT so that you're only seeing one entry per multitude of loads. From there, customers can select the load type they want to work with and it goes into more detail, such as who trucked the different loads in, where it came from, where it's going to, references, that kinda stuff.
My question is, would it be feasible to show how many loads there are in total per distinct select? For instance, the top entry actually has 14 loads that are similar to that entry. How would I go about showing it has that total of 14 loads that could be shown?
in my asp.net+vb web there is a gridview in which i use a radiobuttion list as selectparameter. it works fine
there is a label in that page with code behind as under
Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged Label1.Text = GridView1.Rows.Count.ToString() End Sub count gets displayed but
if i select the first one at that time it will be 0
when i select the second one that time it shows the count of first selection
when i select the third one at that time it shows the count of second selection
I am using a grid view and a textbox(out of gridview to display the total rows in gridview) , i am doing Paging in GridView , and pageSize is set to 5 ; so far i did this
protected void ExistingMappedGrid_OnRowDataBound(object sender, GridViewRowEventArgs e) { totalRowsLabel.Text = "Total " + ExistingMappedGrid.Rows.Count + " records."; } it shows the number of records , but not total ! it shows only the records displaying on the page .
e.g.
if i have a total of 8 records , not page index works and shows 5 rows in gridview and other 3 rows on 2nd page index , but if on first instane page shows 5 records in gridview then textbox shows 5 and when i click on second page index it shows 3 rows ( THAT"S NOT WHAT I CODED AND NOT NEEDED ) i want total rows to be shown
i have design a web application having suppliers table using SqlDatasource..
i have a master page in my design and other form are bound to a context menu..
Now i have displayed the records in the gridview..all i wanted is after i select a record in the gridview it will display the selected record in detailview from another aspx form..
i was able to create the link to another aspx form but the data that it display is the first data from the gridview not the data that i select.
I tried with no succcess doing a search here on the forum for some answers or pointers. So here goes.
I would like to Calculate total time from a records in a table.
the Table we'll call it "Time Table" may have four rows IE.
Field1 Field2 IN 08:00:00 OUT 12:30:00 IN 13:00:00 OUT 17:00:00
I am trying to display these records in a Child gridview with a total. Is there any SQL that can total time like this? or any other way I can total this?
I have data grid with page size 15. I need to find total number of record(s) in a grid. How to do it?There is two ways to do:
1) Here I have used LINQ. So calling stored procedure again and we can done using .Count () property. But here I don't want make call to data base again.2) Creating our own logic like here I have done like:
I'm facing with an old problem that it made me confuse very much. So I need your advice to make sure that I've been using the right way. My demand is to count the number of visitor in my website, so I've coded in Global.asax file:
[Code]....
Then, I used variable Application["SiteHitCounter"] and Application[CurrentUsers"] in another C# behind code file to show them on web page.The problem I'm facing is that the website can't show right total visitor number as in my database when I publish it to shared host.
I am creating a web app that using a DataList for display and editing purposes. A datareader is used to populate the DataList. I have the total number of records in the Datalist being displayed in the Footer. In the DataList, I have a checkbox. How can I iterate thru each record in the Datalist to get the number of checkboxes that are checked?
I'm using a stored procedure in a sql database as the data source for a SqlDataSourceControl on my .aspx page. I'm then using the SqlDataSourceControl as the data source for a gridview on my page. Paging is set to true on the gridview. What i would like to do is set the text of a label to the total number of rows in the gridview. I can use this code
How to display the total number of records returned in a GridView in asp.net? I want to display it in a label or if possible next to the page index in GridView... And how to provide a select option in a GridView to select all records (It should select not only the record shown in one page in GridView but all records that were returned to the GridView) or one by one record that was returned? These things (for e.g., Number of selected rows) should also be displayed in the label which I mentioned before.