Checkbox in gridview in asp.net


Description:-

In this article we will see how to use Checkbox control in Gridview to select particular row of Gridview control. Here I have given simple example to select Gridview row and display in label control what I have selected in Gridview. Using the GridviewRow we can access the grid data in function to get id of that selected row.

In my example I have bind Gridview from database and using the function I have count the selected row from Gridview. Using the GridviewRow class we can get the row from Gridview I have done like this inside Gridview I have create checkbox control to select row and based on selection I have return row in label.

Create Table:-
Create table and Name it "Stores".

CREATE TABLE [dbo].[stores](
 [stor_id] [int] IDENTITY(1,1) NOT NULL,
 [stor_name] [varchar](50) NOT NULL,
 [state] [varchar](50) NOT NULL,
 CONSTRAINT [PK_stores] PRIMARY KEY CLUSTERED 
(
 [stor_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

Default.aspx:-

<div>
  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
  <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  <AlternatingRowStyle BackColor="#BFE4FF" />
    <Columns>
      <asp:TemplateField>
        <ItemTemplate>
          <asp:CheckBox ID="chkCtrl" runat="server" />
        </ItemTemplate>
      </asp:TemplateField>
      <asp:BoundField DataField="stor_id" HeaderText="Stor ID" ItemStyle-Width="70" />
      <asp:BoundField DataField="stor_name" HeaderText="Stor Name" ItemStyle-Width="150" />
      <asp:BoundField DataField="state" HeaderText="State" ItemStyle-Width="50" />
    </Columns>
  </asp:GridView>
  <br />
  <asp:Button ID="btnDisplay" runat="server" Text="Show selected Data" OnClick="btmDisplay_Click" />
  <br />
  <br />
  <asp:Label ID="lblmsg" runat="server" />
</div>

Default.aspx.cs:-

protected void Page_Load(object sender, EventArgs e)
{
  if (!this.IsPostBack)
  {
    SqlDataAdapter adapter = new SqlDataAdapter();
    DataSet ds = new DataSet();
    string sql = null;
    string connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=True";
    sql = "select stor_id,stor_name,state from stores";
    SqlConnection connection = new SqlConnection(connetionString);
    connection.Open();
    SqlCommand command = new SqlCommand(sql, connection);
    adapter.SelectCommand = command;
    adapter.Fill(ds);
    adapter.Dispose();
    command.Dispose();
    connection.Close();
    GridView1.DataSource = ds.Tables[0];
    GridView1.DataBind();
  }
}

protected void btmDisplay_Click(object sender, EventArgs e)
{
  string data = "";
  foreach (GridViewRow row in GridView1.Rows)
  {
    if (row.RowType == DataControlRowType.DataRow)
    {
      CheckBox chkRow = (row.Cells[0].FindControl("chkCtrl") as CheckBox);
      if (chkRow.Checked)
      {
        string storid = row.Cells[1].Text;
        string storname = row.Cells[2].Text;
        string state = row.Cells[3].Text;
        data = data + storid + " ,  " + storname + " , " + state + "<br>";
      }
    }
  }
  lblmsg.Text = data;
}

Now run your application and select which row you have to select then click on button to get selected row.

Related Posts

Previous
Next Post »

Thanks for comments.....