Get count of specific rows based on column value from DataTable in C# .Net

Last Reply on Aug 09, 2014 04:14 AM By Azim

Posted on Aug 09, 2014 03:50 AM

 

DataSet ds = new DataSet();
  string vee = "select id,Name,Section,remaining,nextdate,Status from salary where id = '" + TextBox1.Text + "' AND  nextdate BETWEEN '" + TextBox2.Text + "' and '" + TextBox2.Text + "' AND lastdate BETWEEN '" + TextBox4.Text + "' and '" + TextBox4.Text + "' and Status='" + TextBox3.Text + "'";
        
       ds = mvl.GETDS(vee);
       GridView1.DataSource = ds;
        GridView1.DataBind();
       int count = 0;
        foreach (GridViewRow row in this.GridView1.Rows)
        {
            if (row.Cells[5].Text == "Present")
            {
                count++;
            }
        }
        this.Label3.Text = count.ToString();
        Label3.ForeColor = System.Drawing.Color.LightPink;

 

my sql data is

Section  	nextdate	Status		lastdate
BM		10/08/2014	Present		31/8/2014
BM		01/08/2014	Present		31/8/2014
BM		09/08/2014	Present		31/8/2014
BM		09/08/2014	Present		31/8/2014
BM		09/08/2014	Present		31/8/2014

but my output in local server ishttp://s1279.photobucket.com/user/celvs/media/grid_zps383a6804.png.html?filters[user]=140385911&filters[recent]=1&sort=1&o=0

 

 


in label it show 3 days but output is 4 days

Posted on Aug 09, 2014 04:14 AM Modified on on Aug 09, 2014 04:53 AM

Please refer this code

HTML

<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
        <asp:BoundField DataField="Status" HeaderText="Status" ItemStyle-Width="150" />
    </Columns>
</asp:GridView>
<asp:Label ID="lblTotalPresent" Text="" runat="server" />

Namespace

using System.Data;

 C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Id", typeof(int)),
                    new DataColumn("Name", typeof(string)),
                    new DataColumn("Country",typeof(string)),
                    new DataColumn("Status",typeof(string))});
        dt.Rows.Add(1, "John Hammond", "United States", "Present");
        dt.Rows.Add(2, "Mudassar Khan", "India", "Present");
        dt.Rows.Add(3, "Suzanne Mathews", "France", "Absent");
        dt.Rows.Add(4, "Robert Schidner", "Russia", "Present");
        GridView1.DataSource = dt;
        GridView1.DataBind();
        int count = dt.Select().Where(s => s["Status"].ToString().ToUpper() == "PRESENT").Count();
        this.lblTotalPresent.Text = count.ToString();
    }
}

 If you are using DataSet use this line of code

int count = ds.Tables[0].Select().Where(s => s["Status"].ToString().ToUpper() == "PRESENT").Count();