How to Check Multiple Rows from GridView and retrieve Data in Asp.Net



If you want to check multiple rows in Grid view and retrieve data on checked rows and pass to other form. You can do it in dot net. Using check box controls. Let’s start our discussion.
Here we will add check box controls in GridView Template Field so we can easily check multiple rows from Grid view and for retrieve we will create button and on button click event we will check in Grid view control how many rows are selected.

On webpage Create Grid view control and in Template Field add Checkbox Controls and in Footer add button control for getting selected rows from Grid view.

<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 Data" OnClick="btmDisplay_Click" />
<br />
<br />
<asp:Label ID="lblmsg" runat="server" />
</div>

Now go to code behind and Bind data in Grid view.

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’;Persist Security Info=True;User ID=’userName’;Password=’Password’";
   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();
 }
}
Now generate button Click event and code for Checking Selected row in Gridview.

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 Webpage and Select Multiple row in Grid view and Click button for getting selected rows from Grid view.

Related Posts

Previous
Next Post »

Thanks for comments.....