Hi Vincenzo67,
Refer below code.
HTML
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="btselect" runat="server" OnClick="cmdCopy_Click" AlternateText="Copy" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
if (ViewState["Customers"] == null)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("CustomerId", typeof(int)),
new DataColumn("Name"),
new DataColumn("Country")
});
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
ViewState["Customers"] = dt;
}
this.gvCustomers.DataSource = ViewState["Customers"] as DataTable;
this.gvCustomers.DataBind();
}
protected void cmdCopy_Click(object sender, EventArgs e)
{
GridViewRow gvRow = (sender as ImageButton).NamingContainer as GridViewRow;
int index = gvRow.RowIndex;
int id = Convert.ToInt32(gvRow.Cells[0].Text);
string name = gvRow.Cells[1].Text;
string country = gvRow.Cells[2].Text;
if (ViewState["Customers"] != null)
{
DataTable dt = ViewState["Customers"] as DataTable;
DataRow dr = dt.NewRow();
dr["CustomerId"] = id;
dr["Name"] = name;
dr["Country"] = country;
dt.Rows.InsertAt(dr, index + 1);
ViewState["Customers"] = dt;
}
this.BindGrid();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
If ViewState("Customers") Is Nothing Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("CustomerId", GetType(Integer)),
New DataColumn("Name"),
New DataColumn("Country")
})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
ViewState("Customers") = dt
End If
Me.gvCustomers.DataSource = TryCast(ViewState("Customers"), DataTable)
Me.gvCustomers.DataBind()
End Sub
Protected Sub cmdCopy_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim gvRow As GridViewRow = TryCast((TryCast(sender, ImageButton)).NamingContainer, GridViewRow)
Dim index As Integer = gvRow.RowIndex
Dim id As Integer = Convert.ToInt32(gvRow.Cells(0).Text)
Dim name As String = gvRow.Cells(1).Text
Dim country As String = gvRow.Cells(2).Text
If ViewState("Customers") IsNot Nothing Then
Dim dt As DataTable = TryCast(ViewState("Customers"), DataTable)
Dim dr As DataRow = dt.NewRow()
dr("CustomerId") = id
dr("Name") = name
dr("Country") = country
dt.Rows.InsertAt(dr, index + 1)
ViewState("Customers") = dt
End If
Me.BindGrid()
End Sub
Screenshot