REA_ANDREW
asked on
Record Count
I am trying to fill a span with the record count of my grid view. It kinda of works. But here is the thing. I have added paging displaying 10 records per page. The record count is for each page. So it keeps being 10, until the last page where there are 3 records and the record count is three.
I need the record count to reflect the whole recordset not just the records per page. The simple code I am using at the mo is this
int RecCount = GridView1.Rows.Count;
string WrRecCount = RecCount.ToString();
totalRecords.InnerHtml = WrRecCount;
I need the record count to reflect the whole recordset not just the records per page. The simple code I am using at the mo is this
int RecCount = GridView1.Rows.Count;
string WrRecCount = RecCount.ToString();
totalRecords.InnerHtml = WrRecCount;
if you are filling the gridview with a dataset you can do the count from the dataset or datatable!
ASKER
Do you know the code to do this?
sure... can post the portion of code where you fill the gridview?
ASKER
<asp:GridView ID="GridView1" DataSourceID="SqlDataSourc e1" runat=server />
<asp:SqlDataSource ID="SqlDataSource1" runat=server SelectCommand="SELECT [au_id],[au_lname],[au_fna me],[phone ],[address ],[city],[ state],[zi p],[contra ct] FROM [authors]" ConnectionString="<%$Conne ctionStrin gs:pubsCon n1%>" />
<asp:SqlDataSource ID="SqlDataSource1" runat=server SelectCommand="SELECT [au_id],[au_lname],[au_fna
in the SqlDataSourceStatusEventAr gs is the property AffectedRows that have the number of rows
ASKER
I have read that but it does not work.
You can try executing a seperate SQL command to get the total count i.e. "Select Count(ID) from TABLE". Set the count equal to a variable and that will be your total number of records. True, this makes to DB calls, but you could also execute 2 selects with one command, but then you'd have to use a data adapter and dataset to access the two command results.
ASKER
THanks,
I am aware that I can do it in SQL, but I am just flabbergasted that there is no straighforward way to count the number of records in a result generated by an SQL Statement.
I have searched the google for hours looking and for me that is too long for such a simple task, Or May Be no so simple.
Anyone else have any ideas? Or links of interest
I am aware that I can do it in SQL, but I am just flabbergasted that there is no straighforward way to count the number of records in a result generated by an SQL Statement.
I have searched the google for hours looking and for me that is too long for such a simple task, Or May Be no so simple.
Anyone else have any ideas? Or links of interest
What is the SQL statement you are currently executing?
ASKER
<asp:GridView ID="GridView1" DataSourceID="SqlDataSourc e1" runat=server />
<asp:SqlDataSource ID="SqlDataSource1" runat=server SelectCommand="SELECT [au_id],[au_lname],[au_fna me],[phone ],[address ],[city],[ state],[zi p],[contra ct] FROM [authors]" ConnectionString="<%$Conne ctionStrin gs:pubsCon n1%>" />
<asp:SqlDataSource ID="SqlDataSource1" runat=server SelectCommand="SELECT [au_id],[au_lname],[au_fna
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I know this is closed but I have a followup question on the accepted solution. If i need to open an new question I will but this seems like the place to start.
I used this method (I am using the VB) and it really doesn't work for me. When I try to change the page i get the exception "The GridView 'MyGrid' fired event PageIndexChanging which wasn't handled." MyGrid has properties AllowPaging set to "True" and PageSize = "10". This problem still occurs even after I comment out the code lines to where I am not trying to change any paging property values, and have reloaded the page. In other words it is now completed broken. I have even closed the project & Visual Studio, reopened...nada will not work at all now.
Also even though I think I am creating the label text BEFORE the paging properties are reset, the label only displays "20..." i.e. the page size, unless the number of returns is less of course.
Anyone know why and how to fix it? thanks
I used this method (I am using the VB) and it really doesn't work for me. When I try to change the page i get the exception "The GridView 'MyGrid' fired event PageIndexChanging which wasn't handled." MyGrid has properties AllowPaging set to "True" and PageSize = "10". This problem still occurs even after I comment out the code lines to where I am not trying to change any paging property values, and have reloaded the page. In other words it is now completed broken. I have even closed the project & Visual Studio, reopened...nada will not work at all now.
Also even though I think I am creating the label text BEFORE the paging properties are reset, the label only displays "20..." i.e. the page size, unless the number of returns is less of course.
Anyone know why and how to fix it? thanks
With MyGrid
.AllowPaging = False
i = .Rows.Count()
Label1.Text = i & " records returned"
.AllowPaging = True
.PageSize = 20
End With
Just wondering did you implement PageIndexChanging event for the grid?
...
aspx page...
<asp:GridView ID="MyGrid" OnPageIndexChanging="MyGri d_PageInde xChanging" DataSourceID="SqlDataSourc e1" runat=server />
code behind...
Protected Sub MyGrid_PageIndexChanging(B yVal sender As Object, ByVal e As GridViewPageEventArgs)
MyGrid.PageIndex = e.NewPageIndex
MyGrid.DataBind()
End Sub
...
aspx page...
<asp:GridView ID="MyGrid" OnPageIndexChanging="MyGri
code behind...
Protected Sub MyGrid_PageIndexChanging(B
MyGrid.PageIndex = e.NewPageIndex
MyGrid.DataBind()
End Sub
OK that took care of the first part. However there are a few other issues here so I think it would be better to start a new question.