Click here to Skip to main content
11,934,783 members (41,781 online)
Rate this:
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.

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;

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" />
<asp:BoundField DataField="job_title" HeaderText="Job Title" SortExpression="job_title" />
            <asp:TemplateField HeaderText="No of Applicants" SortExpression="total_applicants">
                    <asp:LinkButton ID="LinkButtonApp" runat="server" CommandName="App" CommandArgument='<%#Eval("job_id")%>' >Applicants
Posted 2-Apr-13 9:30am
Edited 2-Apr-13 10:06am
Rate this: bad
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();

richcb 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 ;)
Maciej Los 3-Apr-13 1:36am
Thank you, Sergey ;)
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Rate this: bad
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)
It is just a simple way to count the rows that will be filling your GridView.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 6 Apr 2013
Copyright © CodeProject, 1999-2015
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