Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi,My Question is
 
How to Count Records in GridView,I took one link button & text of linkbutton is applicants,but i want number of applicants(count applicants)should be display instead of writing applicants.
 
Coding
UI Code
public partial class Job_Response : System.Web.UI.Page
{
    static DataTable dt;
    ResponseDetail_select responseobj = new ResponseDetail_select(ConfigurationManager.ConnectionStrings["portal"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dt = responseobj.get_response_job(cmp_id);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
}
 
Class.cs
public DataTable get_response_job(int cmp_id)
{
  return (dl.GetDataTable("select job_id,job_title from Recruiter_JobPost where company_id='" + cmp_id + "'"));
}
 
Source Code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                DataKeyNames="job_id" onselectedindexchanged="GridView1_SelectedIndexChanged" 
                Height="204px" Width="819px" onrowcommand="GridView1_RowCommand" 
                BorderColor="#0099FF" BorderStyle="Outset" CellPadding="4" ForeColor="#333333" 
                GridLines="None" style="margin-left: 85px" >
        <alternatingrowstyle backcolor="White" />
        <columns>
<asp:BoundField DataField="job_title" HeaderText="Job Title" SortExpression="job_title" />
            <asp:TemplateField HeaderText="No of Applicants" SortExpression="total_applicants">
                
                <itemtemplate>
                    <asp:LinkButton ID="LinkButtonApp" runat="server" CommandName="App" CommandArgument='<%#Eval("job_id")%>' >Applicants
                </itemtemplate>
            
</columns>
Posted 2-Apr-13 9:30am
Edited 2-Apr-13 10:06am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you bind DataTable (dt) to your GridView, you can use Rows.Count property for dt.
 
Label1.Text = "Rows count: " + dt.Rows.Count.ToString();
 
Simple?
  Permalink  
Comments
richcb at 2-Apr-13 15:16pm
   
That is way better than my solution. How could I have forgot about that property when I use it all the time.
Maciej Los at 2-Apr-13 15:18pm
   
Sometimes it happens ;)
Sergey Alexandrovich Kryukov at 2-Apr-13 18:16pm
   
5ed.
—SA
Maciej Los at 3-Apr-13 1:36am
   
Thank you, Sergey ;)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I would try something like this:
     int i = 0;
     
     foreach(DataRow dr in dt.Rows)
     {
         i++;
     }
It is just a simple way to count the rows that will be filling your GridView.
  Permalink  
Comments
Maciej Los at 2-Apr-13 15:15pm
   
What's for go thru the collection of rows? See my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 325
1 DamithSL 300
2 Sergey Alexandrovich Kryukov 289
3 CPallini 235
4 Maciej Los 215
0 OriginalGriff 5,455
1 DamithSL 4,422
2 Maciej Los 3,860
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,010


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 6 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100