ASP.NET – Create GridView with Checkbox

  • by
checkboxes2

Gridview checkbox in Asp.net c#

Its pretty simple to put checkboxes in gridview control in asp.net.

DESIGN OF THE PAGE :

Open your .aspx page and add a gridview control like below and its good design 🙂 :

<asp:GridView runat="server" ID="datagridview" CssClass="mydatagrid" AllowPaging="True"
CellPadding="4" ForeColor="#333333" GridLines="None" BorderColor="#003300" BorderStyle="Solid"
BorderWidth="1px" Font-Size="11pt" Font-Names="Century" OnPageIndexChanging="datagridview_PageIndexChanging">
<RowStyle BackColor="#EFF3FB" BorderColor="Black" BorderWidth="1px" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" />
<AlternatingRowStyle BackColor="White" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" />
<Columns>
<asp:TemplateField AccessibleHeaderText="Select" HeaderText="Select">
<ItemTemplate>
<asp:CheckBox runat="server" ID="chk" Enabled="true" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

add two more controls : Labels and Button to get the values :

<br />
<asp:Button Text="Count" ID="btncount" BackColor="White" ForeColor="#507CD1" runat="server"
Font-Bold="true" Font-Size="15px" Height="50px" Width="150px" Enabled="true"
Visible="true" BorderColor="#507CD1" BorderStyle="Solid" BorderWidth="2px"
onclick="btncount_Click" />
<asp:Label runat="server" Text="Click to get count" ID="lblcount"
ForeColor="#507CD1" Font-Size="20px"></asp:Label>
<br />
<asp:Label runat="server" Text="YOU SELECTED - " ID="Label1"
ForeColor="#507CD1" Font-Size="20px"></asp:Label>

<asp:Label runat="server" Text="" ID="lblname"
ForeColor="#507CD1" Font-Size="20px"></asp:Label>

It should look like this :

checkboxes

CODING PART

Open your Web.config file and define your connectionString :

<connectionStrings>
<add name="myconnectionString" connectionString="Data Source=HITESH\SQLEXPRESS;Initial Catalog=MyDatabase;User ID=hitesh;Password=789"/>
</connectionStrings>

Open code file .cs file

and write the code to fill the GridView

public void fillDataGrid()
{
con = new SqlConnection(myconnectionString);
cmd = new SqlCommand("select * from mytable", con);
con.Open();
adapter = new SqlDataAdapter(cmd);
ds = new DataSet();
adapter.Fill(ds, "mytable");
datagridview.DataSource = ds.Tables[0];
datagridview.DataBind();
con.Close();
}

Call this method on PageLoad event

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) // make sure to write this to avoid reload of whole page
{
fillDataGrid();
}

}

Now write the code for our button click event like this :

protected void btncount_Click(object sender, EventArgs e)
{
int i = 0;
lblname.Text = "";
foreach (GridViewRow row in datagridview.Rows) //getting each row
{
CheckBox Checkbox = row.FindControl("chk") as CheckBox;

if (Checkbox.Checked == true) // checking whether the checkbox is checked or not
{

i = i + 1; // counting how many are checked
String name = row.Cells[2].Text.ToString(); // getting the desired data
lblcount.Text = "You checked " + Convert.ToString(i) + " Product(s)."; // setting the text
if (i == 1)
{
lblname.Text = name; // its logic to avoid commas for the first value
}
else
{
lblname.Text = lblname.Text.ToString() + ", " + name;
}

}
}
}

Now when you run this code :

image

checkboxes2

If you like my work, please click the like button and like my page on facebook. You can also comment below…I will try my best to help you out

Here’s the whole code :

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data.Sql;
using System.IO;
using System.Text;

public partial class GridViewCheckBox : System.Web.UI.Page
{
String myconnectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
SqlConnection con;
SqlDataAdapter adapter;
DataSet ds;
SqlCommand cmd;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillDataGrid();
}

}
public void fillDataGrid()
{
con = new SqlConnection(myconnectionString);
cmd = new SqlCommand("select * from mytable", con);
con.Open();
adapter = new SqlDataAdapter(cmd);
ds = new DataSet();
adapter.Fill(ds, "mytable");
datagridview.DataSource = ds.Tables[0];
datagridview.DataBind();
con.Close();
}
protected void datagridview_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
datagridview.PageIndex = e.NewPageIndex;
fillDataGrid();
}
protected void btncount_Click(object sender, EventArgs e)
{
int i = 0;
lblname.Text = "";
foreach (GridViewRow row in datagridview.Rows)
{
CheckBox Checkbox = row.FindControl("chk") as CheckBox;

if (Checkbox.Checked == true)
{

i = i + 1;
String name = row.Cells[2].Text.ToString();
lblcount.Text = "You checked " + Convert.ToString(i) + " Product(s).";
if (i == 1)
{
lblname.Text = name;
}
else
{
lblname.Text = lblname.Text.ToString() + ", " + name;
}

}
}
}
}


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.