none
Count Gridview rows...

Answers

  • 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:

    label1.Text = this.gridView1.Rows.Count.ToString();

    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)

    Tuesday, March 06, 2007 2:24 AM

All replies

  • int numberOfRows = testGrid.Rows.Count;

    Wednesday, February 21, 2007 2:57 PM
  • Where in the code do I insert this?
    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?
    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.
    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:

    label1.Text = this.gridView1.Rows.Count.ToString();

    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();

     

    Saturday, February 24, 2007 6:51 PM
  • What tags do I use and where in the HTML code does this go?
    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.

    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 by Sniper777 Friday, July 31, 2009 2:36 AM
    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)

    Tuesday, March 06, 2007 2:24 AM
  • this worked for me thanks Zhi-Xin Ye.
    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 a checkbox "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>

     

     

     



    Friday, July 31, 2009 2:42 AM
  • I agree with you there Sniper777.
    Friday, July 31, 2009 8:08 PM
  • If paging is needed/enabled on the Grid and you want to show the total number of records including those on all other pages of the grid, then here is a simple solution...

    'setup the datasource for the grid
    GridView1.DataSource = CustomDataSource

    'Optionally, if you are using an SQL data source, please ensure the name of the SQL data source object is in quotes
    GridView1.DataSourceId = "SQLDataSourceObjectName"


    'disable paging
    GridView1.AllowPaging = False

    'bind the data to the grid
    GridView1.DataBind()

    'display the row count in a label (or any other output form you choose)
    lblCount.Text = "Records returned: " & GridView1.Rows.Count 

    're-enable paging 
    GridView1.AllowPaging = True


    Friday, March 26, 2010 2:06 PM
  • this will only return the number of records on the current page... totally inaccurate if paging is enabled and there are multiple pages in the gridview results set.
    Friday, March 26, 2010 2:07 PM
  • this will only return the number of records on the current page... totally inaccurate if paging is enabled and there are multiple pages in the gridview results set.

    As Chevy B noted, this will effectively show you the GridView row count property.  If paging is enabled your dataset runs longer than the page, it's meaningless.  Guessing you might be looking for the data set count value, I suggest using the SqlDataSource_Selected event with a datasource, then, set retrieve the e.AffectedRows.ToString() approach:

    protected void SqlDataSource_Selected(object sender, SqlDataSourceStatusEventArgs e)

    {

    LabelRecordCount.Text = e.AffectRows.ToString();

    }

    This assumes you are actually looking for the record in the dataset, not the row count of the GridView.

    • Proposed as answer by Hannibal777 Wednesday, April 06, 2011 1:59 PM
    Friday, September 17, 2010 1:37 PM
  • I have two tables in asp.net+vb+sql 2005. PERSA and PRUN are tables & PNUMB and CDPR are fields of PERSA and PNUMB and SPECI are field of PRUN I want to count numbers as per the following data conditions.

     

    PERSA.PNUM, PERSA. CDPR=4 and PRUN.SPECI=SPEC AND PERSA.PNUMB= @ PRUN.PNUMB

    PERSA. CDPR=4 and PRUN.SPECI=MS1 AND PERSA.PNUMB= @ PRUN.PNUMB

    PERSA. CDPR=4 and PRUN.SPECI=MS3 AND PERSA.PNUMB= @ PRUN.PNUMB

    PERSA. CDPR=4 and PRUN.SPECI=SL AND PERSA.PNUMB= @ PRUN.PNUMB

    PERSA. CDPR=4 and PRUN.SPECI=STRO AND PERSA.PNUMB= @ PRUN.PNUMB

     PERSA. CDPR=4 and PRUN.SPECI=COUR AND PERSA.PNUMB= @ PRUN.PNUMB

     all the labels to display are in same page and the script is in VB


    I am a System Administrator at Vadodara
    Saturday, September 10, 2011 4:28 PM
  • Thanks Will Steele it work :)
    Thursday, May 31, 2012 10:00 AM
  • This is the exact answer!!!

    Thank You very much!!!!!

    • Proposed as answer by UkrTiger Friday, May 31, 2013 7:30 PM
    • Unproposed as answer by UkrTiger Friday, May 31, 2013 7:30 PM
    Friday, May 31, 2013 7:29 PM