Hi amar,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="gvShow" runat="server" AutoGenerateColumns="false" AutoGenerateSelectButton="true"
OnSelectedIndexChanged="OnSelectedIndexChanged">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<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("Id", 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");
gvShow.DataSource = dt;
gvShow.DataBind();
for (int i = 1; i < gvShow.Rows.Count; i++)
{
gvShow.Rows[i].Visible = false;
}
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = gvShow.SelectedIndex;
for (int i = 0; i <= selectedIndex; i++)
{
if (gvShow.Rows.Count > selectedIndex + 1)
{
gvShow.Rows[i + 1].Visible = true;
}
}
}
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("Id", 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")
gvShow.DataSource = dt
gvShow.DataBind()
For i As Integer = 1 To gvShow.Rows.Count - 1
gvShow.Rows(i).Visible = False
Next
End If
End Sub
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim selectedIndex As Integer = gvShow.SelectedIndex
For i As Integer = 0 To selectedIndex
If gvShow.Rows.Count > selectedIndex + 1 Then
gvShow.Rows(i + 1).Visible = True
End If
Next
End Sub
Screenshot