Hi smile,
Refer below sample.
HTML
<asp:GridView ID="gvRecord" runat="server" AutoGenerateColumns="false" Class="table table-striped table-bordered table-hover"
OnRowDeleting="gvRecord_RowDeleting" OnSelectedIndexChanged="gvRecord_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:CommandField ShowDeleteButton="True" ButtonType="Button" ControlStyle-CssClass="btn btn-danger"
ShowSelectButton="true" />
</Columns>
</asp:GridView>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Class="table table-striped table-bordered table-hover">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</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)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("CustomerId", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
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");
gvRecord.DataSource = dt;
gvRecord.DataBind();
}
}
protected void gvRecord_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = gvRecord.Rows[e.RowIndex];
int id = Convert.ToInt32(row.Cells[0].Text);
int index = Convert.ToInt32(e.RowIndex);
DataTable deleteRow = (ViewState["dt"] as DataTable).DefaultView.ToTable(true);
DataRow[] dr = deleteRow.Select("CustomerId = " + id);
foreach (DataRow d in dr)
{
deleteRow.Rows.Remove(d);
}
ViewState["dt"] = deleteRow;
this.GridView1.DataSource = (ViewState["dt"] as DataTable).DefaultView.ToTable(true);
this.GridView1.DataBind();
}
protected void gvRecord_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = gvRecord.SelectedRow;
int id = Convert.ToInt32(row.Cells[0].Text);
string name = row.Cells[1].Text;
string country = row.Cells[2].Text;
if (ViewState["dt"] == null)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("CustomerId", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(id, name, country);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
ViewState["dt"] = dt;
}
else
{
DataTable dt1 = ViewState["dt"] as DataTable;
dt1.Rows.Add(id, name, country);
this.GridView1.DataSource = dt1;
this.GridView1.DataBind();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("CustomerId", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
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")
gvRecord.DataSource = dt
gvRecord.DataBind()
End If
End Sub
Protected Sub gvRecord_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
Dim row As GridViewRow = gvRecord.Rows(e.RowIndex)
Dim id As Integer = Convert.ToInt32(row.Cells(0).Text)
Dim index As Integer = Convert.ToInt32(e.RowIndex)
Dim deleteRow As DataTable = (TryCast(ViewState("dt"), DataTable)).DefaultView.ToTable(True)
Dim dr As DataRow() = deleteRow.[Select]("CustomerId = " & id)
For Each d As DataRow In dr
deleteRow.Rows.Remove(d)
Next
ViewState("dt") = deleteRow
Me.GridView1.DataSource = (TryCast(ViewState("dt"), DataTable)).DefaultView.ToTable(True)
Me.GridView1.DataBind()
End Sub
Protected Sub gvRecord_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = gvRecord.SelectedRow
Dim id As Integer = Convert.ToInt32(row.Cells(0).Text)
Dim name As String = row.Cells(1).Text
Dim country As String = row.Cells(2).Text
If ViewState("dt") Is Nothing Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("CustomerId", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
dt.Rows.Add(id, name, country)
Me.GridView1.DataSource = dt
Me.GridView1.DataBind()
ViewState("dt") = dt
Else
Dim dt1 As DataTable = TryCast(ViewState("dt"), DataTable)
dt1.Rows.Add(id, name, country)
Me.GridView1.DataSource = dt1
Me.GridView1.DataBind()
End If
End Sub
Screenshot