Hi in this tutorial explains how to get selected rows from gridview using asp dot net Checkbox control.Open your MsSql server and create a database with name "User" and then create a table "userRegistration".
Html Tag Code:
<asp:GridView ID="grdViewDemo" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField DataField="id" HeaderText="Id" />
<asp:BoundField DataField="name" HeaderText="Name" />
<asp:BoundField DataField="Address" HeaderText="Address" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="State" HeaderText="State" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:CheckBox ID="checkBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnFetch" runat="server" CssClass="btn btn-warning" OnClick="btnFetch_Click" Text="Fetch Row" />
<br />
<asp:GridView ID="grdVide" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField DataField ="id" HeaderText="id" />
<asp:BoundField DataField="name" HeaderText="Name" />
<asp:BoundField DataField="Address" HeaderText="Address" />
</Columns>
</asp:GridView>
BindData To GridView:
Code to Get Data From Selected Check box and Insert it into another GridView:
Output:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindData();
}
}
private string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
private void BindData()
{
SqlConnection conn = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand();
string sqlQuery = "Select *FROM tblUserRegistration";
cmd.Connection = conn;
cmd.CommandText = sqlQuery;
cmd.CommandType = System.Data.CommandType.Text;
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
grdViewDemo.DataSource = dt;
grdViewDemo.DataBind();
}
{
if (!this.IsPostBack)
{
this.BindData();
}
}
private string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
private void BindData()
{
SqlConnection conn = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand();
string sqlQuery = "Select *FROM tblUserRegistration";
cmd.Connection = conn;
cmd.CommandText = sqlQuery;
cmd.CommandType = System.Data.CommandType.Text;
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
grdViewDemo.DataSource = dt;
grdViewDemo.DataBind();
}
Code to Get Data From Selected Check box and Insert it into another GridView:
protected void btnFetch_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Name");
dt.Columns.Add("Address");
foreach (GridViewRow grdRow in grdViewDemo.Rows)
{
CheckBox chkRow = (grdRow.Cells[6].FindControl("checkBox") as CheckBox);
if (chkRow.Checked)
{
string id = grdRow.Cells[0].Text;
string name = grdRow.Cells[1].Text;
string Address = grdRow.Cells[2].Text;
dt.Rows.Add(id, name, Address);
}
}
grdVide.DataSource = dt;
grdVide.DataBind();
}
{
DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Name");
dt.Columns.Add("Address");
foreach (GridViewRow grdRow in grdViewDemo.Rows)
{
CheckBox chkRow = (grdRow.Cells[6].FindControl("checkBox") as CheckBox);
if (chkRow.Checked)
{
string id = grdRow.Cells[0].Text;
string name = grdRow.Cells[1].Text;
string Address = grdRow.Cells[2].Text;
dt.Rows.Add(id, name, Address);
}
}
grdVide.DataSource = dt;
grdVide.DataBind();
}
Output:
Download Source Code
No comments :
Post a Comment