Hi smile,
Refer the sample code.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Num" HeaderText="Number" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" Text="Call" OnClick="OnCall" />
<hr />
Name: <asp:Label ID="lblName" runat="server" /><br />
Token: <asp:Label ID="lblToken" runat="server" />
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();
}
}
protected void OnCall(object sender, EventArgs e)
{
lblName.Text = string.Empty;
lblToken.Text = string.Empty;
DataTable dt = ViewState["Data"] as DataTable;
if (dt.Rows.Count > 0)
{
lblName.Text = dt.Rows[0][0].ToString();
lblToken.Text = dt.Rows[0][1].ToString();
dt.Rows[0].Delete();
ViewState["Data"] = dt;
this.BindGrid();
}
}
private void BindGrid()
{
DataTable dt = new DataTable();
if (ViewState["Data"] == null)
{
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name"), new DataColumn("Num") });
dt.Rows.Add("Chetak", "12345");
dt.Rows.Add("Vidhan", "13456");
dt.Rows.Add("Satya", "12765");
dt.Rows.Add("Akash", "98345");
dt.Rows.Add("Amit", "56129");
ViewState["Data"] = dt;
}
else
{
dt = ViewState["Data"] as DataTable;
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
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
Protected Sub OnCall(ByVal sender As Object, ByVal e As EventArgs)
lblName.Text = String.Empty
lblToken.Text = String.Empty
Dim dt As DataTable = TryCast(ViewState("Data"), DataTable)
If dt.Rows.Count > 0 Then
lblName.Text = dt.Rows(0)(0).ToString()
lblToken.Text = dt.Rows(0)(1).ToString()
dt.Rows(0).Delete()
ViewState("Data") = dt
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim dt As DataTable = New DataTable()
If ViewState("Data") Is Nothing Then
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Name"), New DataColumn("Num")})
dt.Rows.Add("Chetak", "12345")
dt.Rows.Add("Vidhan", "13456")
dt.Rows.Add("Satya", "12765")
dt.Rows.Add("Akash", "98345")
dt.Rows.Add("Amit", "56129")
ViewState("Data") = dt
Else
dt = TryCast(ViewState("Data"), DataTable)
End If
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Screenshot