Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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
C#
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.NET
<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
Updated 2-Apr-13 9:06am
v3

 
Share this answer
 
If you bind DataTable (dt) to your GridView, you can use Rows.Count property for dt.

C#
Label1.Text = "Rows count: " + dt.Rows.Count.ToString();


Simple?
 
Share this answer
 
Comments
Richard C Bishop 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 2-Apr-13 15:18pm    
Sometimes it happens ;)
Sergey Alexandrovich Kryukov 2-Apr-13 18:16pm    
5ed.
—SA
Maciej Los 3-Apr-13 1:36am    
Thank you, Sergey ;)
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.
 
Share this answer
 
Comments
Maciej Los 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900