Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Count Gridview rows...
 

Count Gridview rows...

How can I get a count of the rows within a gridview?
Tom T.  Wednesday, February 21, 2007 2:21 AM

Why not use a label to display the row count instead of mixing C# code with HTML? For example, in your cs file, write code like:

this.gridView1.Rows.Count.ToString();

Zhi-Xin Ye  Saturday, February 24, 2007 6:14 AM

I used a custom SQL Statement within the DataGridView Configure Data Source

SELECT COUNT(RecordID) AS CountRecords FROM TableName WHERE (FieldName = @FormFieldName)

Tom T.  Tuesday, March 06, 2007 2:24 AM

int numberOfRows = testGrid.Rows.Count;

oswaldorb  Wednesday, February 21, 2007 2:57 PM
Where in the code do I insert this?
Tom T.  Friday, February 23, 2007 1:54 PM
Well, it really depends where you need to get the count of the rows. Can you explain what are you are trying to accomplish?
oswaldorb  Friday, February 23, 2007 2:13 PM
I have a datagridview that I would like to display the number of records (Rows) returned in the DataGrid. I have paging turned off so all of the records will display on the one page. In frontpage I would just add <%=fp_iCount %> at the end of the page.
Tom T.  Friday, February 23, 2007 8:06 PM

Why not use a label to display the row count instead of mixing C# code with HTML? For example, in your cs file, write code like:

this.gridView1.Rows.Count.ToString();

Zhi-Xin Ye  Saturday, February 24, 2007 6:14 AM

Say if your Grid is GV1 and Label is lbl1

VB.Net:

lbl1.Text = GV1.Rows.Count.ToString()

C#.Net

lbl1.Text = GV1.Rows.Count.ToString();

MANAN_THAKKAR_197d7f  Saturday, February 24, 2007 6:51 PM
What tags do I use and where in the HTML code does this go?
Tom T.  Sunday, February 25, 2007 12:35 AM

Tom T. wrote:
What tags do I use and where in the HTML code does this go?

The code I post is written in the .cs file or .vb file, not in the .aspx file. Just drag a Label onto the aspx page, right cilck on the page, choose "View Code", put the code in it.

Zhi-Xin Ye  Sunday, February 25, 2007 2:39 AM
I do not have a "View Code" when I right click the Label or anywhere on the aspx page. Do I add this code into the Default.vb file even thought the page is page2.aspx and not the default.aspx page? Is there another way to point the label to the code?
  • Proposed As Answer bySniper777 Friday, July 31, 2009 2:36 AM
  •  
Tom T.  Thursday, March 01, 2007 4:22 PM

I used a custom SQL Statement within the DataGridView Configure Data Source

SELECT COUNT(RecordID) AS CountRecords FROM TableName WHERE (FieldName = @FormFieldName)

Tom T.  Tuesday, March 06, 2007 2:24 AM
this worked for me thanks Zhi-Xin Ye.
Rick Martinez  Tuesday, July 28, 2009 3:47 PM
I am not sure why people don't just post complete answers, instead of incomplete thoughts or incomplete code. TOM... check the solution below if you haven't found the answer after 2 years of writing this. I posted the count of my gridview on acheckbox "CheckChanged" event. This will only display the Count of rows on the 1st page if you have Pagination(Allow Paging)= True

.CS
protected void cbxSearchAll_CheckedChanged(object sender, EventArgs e)

{

lblCount.Text = GridView1.Rows.Count.ToString();
}

.aspx - source code
<asp:Label ID="lblCount" runat="server"></asp:Label>



Sniper777  Friday, July 31, 2009 2:42 AM
I agree with you there Sniper777.
Rick Martinez  Friday, July 31, 2009 8:08 PM

You can use google to search for other answers

Custom Search

More Threads

• Two DataGridView controlled by one scrollbar
• what is the procedure to insert the chackbox in the datagrid column?
• Problem with bindingNavigator of two tables. Please help....
• Save datagrid layout to file
• c# EVENTS
• How to get data from Object Data Source in code
• Exporting dataset to Excel
• DataGrid headers
• DataGridView automatic scrolling
• how upload files into application