Move Gridview selected rows from one Gridview to other
Move Gridview selected rows from one Gridview to other
Database Design
Table creation
CREATE TABLE [dbo].[GridView](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NULL,
[age] [int] NULL,
[salary] [decimal](18, 0) NULL,
[country] [varchar](50) NULL,
[city] [varchar](50) NULL,
CONSTRAINT [PK_GridView] PRIMARY KEY CLUSTERED
(
[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]
.aspx part
In design page I am using two gridview,gr1 and gr2.
On selection of rows in 1st gridview it will to
another gridview.
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table>
<tr style="color: red">
<td>GridView 1 Data
</td>
</tr>
<tr>
<td>
<asp:GridView ID="gr1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" OnCheckedChanged="chk_CheckedChanged" AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="Id" />
<asp:BoundField DataField="name" HeaderText="Name" />
<asp:BoundField DataField="age" HeaderText="Age" />
<asp:BoundField DataField="salary" HeaderText="Salary" />
<asp:BoundField DataField="country" HeaderText="Country" />
<asp:BoundField DataField="city" HeaderText="City" />
</Columns>
</asp:GridView>
</td>
</tr>
<tr style="color: red">
<td>GridView 2 Data On selection
basis
</td>
</tr>
<tr>
<td>
<asp:GridView ID="gr2" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="id" HeaderText="Id" />
<asp:BoundField DataField="name" HeaderText="Name" />
<asp:BoundField DataField="age" HeaderText="Age" />
<asp:BoundField DataField="salary" HeaderText="Salary" />
<asp:BoundField DataField="country" HeaderText="Country" />
<asp:BoundField DataField="city" HeaderText="City" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
.Cs part
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind_gr1();
}
}
protected void chk_CheckedChanged(object sender, EventArgs e)
{
GetGr1Rows();
bindgr2();
}
public void bind_gr1()
{
con.Open();
string s = "select top(10) * from
gridview";
SqlDataAdapter ad = new SqlDataAdapter(s, con);
DataTable dt = new DataTable();
ad.Fill(dt);
gr1.DataSource = dt;
gr1.DataBind();
}
protected void bindgr2()
{
DataTable dt = (DataTable)ViewState["Data"];
gr2.DataSource = dt;
gr2.DataBind();
}
private void GetGr1Rows()
{
DataTable dt;
if (ViewState["Data"] != null)
dt = (DataTable)ViewState["Data"];
else
dt = TempTable();
for (int i = 0; i < gr1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)gr1.Rows[i].Cells[0].FindControl("chk");
if (chk.Checked)
{
dt = AddRow(gr1.Rows[i], dt);
}
else
{
dt = RemoveRow(gr1.Rows[i],
dt);
}
}
ViewState["Data"] = dt;
}
private DataTable TempTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("Name");
dt.Columns.Add("age");
dt.Columns.Add("salary");
dt.Columns.Add("country");
dt.Columns.Add("city");
dt.AcceptChanges();
return dt;
}
private DataTable AddRow(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("id
= '" + gvRow.Cells[1].Text + "'");
if (dr.Length <= 0)
{
dt.Rows.Add();
int rowscount = dt.Rows.Count - 1;
dt.Rows[rowscount]["id"] =
gvRow.Cells[1].Text;
dt.Rows[rowscount]["Name"] =
gvRow.Cells[2].Text;
dt.Rows[rowscount]["age"] =
gvRow.Cells[3].Text;
dt.Rows[rowscount]["salary"] =
gvRow.Cells[4].Text;
dt.Rows[rowscount]["country"] =
gvRow.Cells[5].Text;
dt.Rows[rowscount]["city"] =
gvRow.Cells[6].Text;
dt.AcceptChanges();
}
return dt;
}
private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("id
= '" + gvRow.Cells[1].Text + "'");
if (dr.Length > 0)
{
dt.Rows.Remove(dr[0]);
dt.AcceptChanges();
}
return dt;
}
Comments
Post a Comment