how to display total row count from gridview and display in lable
namespace ProjectBarcode
{
public partial class Search : System.Web.UI.Page
{
public SqlConnection con;
public string constr;
public void connection()
{
constr = ConfigurationManager.ConnectionStrings["constr"].ToString();
con = new SqlConnection(constr);
con.Open();
}
protected void Page_Load(object sender, EventArgs e)
{
Label1.Visible = false;
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT distinct Contractor_Name FROM employee order by Contractor_Name "))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
DataTable dt = new DataTable();
dt.Columns.Add("employee");
DataRow dataRow = dt.NewRow();
DataSet customers = new DataSet();
DataTable customersTable = customers.Tables.Add("employee");
dt.Rows.Add(dataRow);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
constr = dt.Rows[i]["employee"].ToString();
}
}
}
con.Close();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
connection();
string query = "select count(b.Workman_ID)as totalRow ,a.Contractor_ID,a.Contractor_Name, b.Workman_ID,b.Name,b.date,b.in_time from employee a, trans b where a.Workman_ID = b.Workman_ID and b.Date = '" + Textbox1.Text + "' group by a.Contractor_Name,a.Contractor_ID,b.Workman_ID,a.Name,b.date,b.in_time ";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
SqlCommand com = new SqlCommand(query, con);
SqlDataReader dr;
dr = com.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
GridView1.Visible = true;
Textbox1.Text = "";
Label1.Text = "";
}
else
{
GridView1.Visible = false;
Label1.Visible = true;
Label1.Text = "The search Term " + Textbox1.Text + " Is Not Available in the Records"; ;
}
lblTotal.Text = ds.Tables[0].Rows[0]["totalRow"].ToString();
}
protected void OnDataBound(object sender, EventArgs e)
{
lblTotal.Text = "Total: " + (GridView1.DataSource as DataTable).Rows.Count;
}
}
}
<asp:TextBox ID="Textbox1" runat="server" Height="25px" ></asp:TextBox>
<asp:Label ID="lblTotal" runat="server" Text=""></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Show" onclick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="label"></asp:Label>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="False" PageSize="10" Width="1035px" horizontalalign="Right" >
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="Contractor_ID" HeaderText="Contractor ID" />
<asp:BoundField ItemStyle-Width="250px" DataField="Contractor_Name" HeaderText="Contractor Name " />
<asp:BoundField ItemStyle-Width="150px" DataField="Workman_ID" HeaderText="Workman ID" />
<asp:BoundField ItemStyle-Width="250px" DataField="name" HeaderText="Workman Name" />
<asp:boundfield datafield="Date" HeaderText="IN Date" DataFormatString="{0:dd/MM/yyyy}" htmlencode="false" />
<asp:TemplateField>
<HeaderTemplate>
IN Time
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("In_time", "{0:t}")%>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>