Friday, November 8, 2013

Adding checkbox in Gridview

Gridview CheckBox


In a form place a Gridview and edit columns. In the TemplateField add a CheckBox.
Add BoundFileds to connect to database values. My table is as follows






 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" 
        CellPadding="3" GridLines="Vertical"   DataKeyNames="rollno">
        <AlternatingRowStyle BackColor="#DCDCDC" />
        <Columns>
            <asp:TemplateField HeaderText="select">
                             <ItemTemplate>
                    <asp:CheckBox ID="chklist" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="rollno" HeaderText="ID" />
            <asp:BoundField DataField="name" HeaderText="Name" />
            <asp:BoundField DataField="marks" HeaderText="Marks" />
        </Columns>
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#0000A9" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#000065" />
    </asp:GridView>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Select" 
        Height="26px" />
    <p>
        <asp:Literal ID="Literal1" runat="server"></asp:Literal>
    </p>
    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"  DataKeyNames="rollno">
        <Columns>
            <asp:TemplateField>
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="rollno" HeaderText="RollNo" />
            <asp:BoundField DataField="name" HeaderText="name" />
            <asp:BoundField DataField="marks" HeaderText="Mark" />
            <asp:CommandField ShowEditButton="True" />
        </Columns>
    </asp:GridView>

Create a Connect Class

using System.Data.SqlClient;
using System.Data;
public class Connect
{
    SqlConnection con;
public Connect()
    {
     con   = new SqlConnection(@"Data Source=.;Initial Catalog=[Dbname];Integrated Security=True");
      
}

    public DataTable Select()
    {
        SqlDataAdapter ad = new SqlDataAdapter("select * from stud", con);
        DataSet ds = new DataSet();
        ad.Fill(ds, "stud");
        return ds.Tables[0];
    }
}


Now In the code behind  create the following code



public partial class _Default : System.Web.UI.Page
{
    Connect c = new Connect();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }
    }

    private void bind()
    {

        GridView1.DataSource = c.Select();
        DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string str = string.Empty;
        string name = ""; 
        foreach (GridViewRow gr in GridView1.Rows)
        {
            CheckBox ck = (CheckBox)gr.FindControl("chklist");
            if (ck != null && ck.Checked )
            {
                str += GridView1.DataKeys[gr.RowIndex].Value.ToString() + "  ";
                name += gr.Cells[2].Text+"  ";
            }
        }
        Literal1.Text ="selected:"+ str + "<br/>" + name;
    }

}


Now Save and Run the program


Enjoy!!

0 comments:

Post a Comment