Insert / Delete / Update Row in DataGrid at Runtime from Other GridView

Grid 1
<asp:GridView ID="gv_Source" runat="server" AutoGenerateColumns="False" OnRowCommand="gv_Source_RowCommand" Width="100%">

<Columns>

<asp:BoundField DataField="TemplateName" HeaderText="Template Name" />

<asp:TemplateField>

<ItemTemplate>

<input type="text" ID="txt_TextBox" runat="server" style="width: 28px" />

<asp:Label ID="lbl_Required" runat="server" Font-Bold="True" ForeColor="Red" Text="*"

Visible="False"></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField>

<ItemTemplate>

<asp:LinkButton ID="lnkbt_SelectTemplates" runat="server" Text="Select" CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' ValidationGroup="SelectGroup"></asp:LinkButton>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

Grid2
<asp:GridView ID="gv_Selected" runat="server" AutoGenerateColumns="False" HorizontalAlign="Left" Width="100%" OnRowCommand="gv_Selected_RowCommand">

<Columns>

<asp:BoundField DataField="Column1" HeaderText="Column Name" />

<asp:BoundField DataField="Column2" HeaderText="Column2 Nam" />

<asp:TemplateField>

<ItemTemplate>

<asp:LinkButton ID="lnkbt_Delete" runat="server" Text="Remove" CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' CausesValidation="False" OnClientClick="return confirm('Are you sure?');"></asp:LinkButton>

</ItemTemplate></asp:TemplateField>

</Columns>

</asp:GridView>
CODE BEHIND
Grid 1 RowCommand (Add to Grid 2)

protected void gv_Source_RowCommand(object sender, GridViewCommandEventArgs e)

{

GridViewRow gv_row = gv_Source.Rows[int.Parse(e.CommandArgument.ToString())];

DataTable dt = new DataTable("Selected");

dt.Columns.Add(

"Column1");

dt.Columns.Add(

"Column2");

dt.AcceptChanges();

DataRow dr = null;

//Check if the grid has already some rows

if (gv_Selected.Rows.Count > 0)

{

foreach (GridViewRow gr in gv_Selected.Rows)

{

dr = dt.NewRow();

dr[0] = gr.Cells[0].Text;

dr[1] = gr.Cells[1].Text;

dt.Rows.Add(dr);

dt.AcceptChanges();

}

}

if (((HtmlInputText)gv_row.FindControl("txt_TextBox")).Value.Length != 0)

{

dr = dt.NewRow();

dr[0] = gv_row.Cells[0].Text;

dr[1] = ((

HtmlInputText)gv_row.FindControl("txt_TextBox")).Value;

dt.Rows.Add(dr);

dt.AcceptChanges();

Label lbl_temp = (Label)gv_row.FindControl("lbl_Required");

lbl_temp.Visible =

false;

}

else

{

Label lbl_temp = (Label)gv_row.FindControl("lbl_Required");

lbl_temp.Visible =

true;

}

gv_Selected.DataSource = dt;

gv_Selected.DataBind();

}

Grid 2 RowCommand (Delete Row)

protected void gv_Selected_RowCommand(object sender, GridViewCommandEventArgs e)

{

DataTable dt = new DataTable("Selected");

dt.Columns.Add(

"Column1");

dt.Columns.Add(

"Column2");

dt.AcceptChanges();

DataRow dr = null;

if (gv_Selected.Rows.Count > 0)

{

for (int i = 0; i < gv_Selected.Rows.Count;i++)

{

GridViewRow gr = gv_SelectedTemplates.Rows[i];

if (i.ToString() != e.CommandArgument.ToString())

{

dr = dt.NewRow();

dr[0] = gr.Cells[0].Text;

dr[1] = gr.Cells[1].Text;

dt.Rows.Add(dr);

dt.AcceptChanges();

}

}

}

gv_SelectedTemplates.DataSource = dt;

gv_SelectedTemplates.DataBind();

}

Comments

Popular posts from this blog

Use configSource attribute to manage Web.Config sections in ASP.Net 2.0

DevOps - Key Concepts - Roles & Responsibilities - Tools & Technologies