Hi amar,
Check this example. Now please take its reference and correct your code.
HTML
<asp:RadioButtonList ID="rblStates" runat="server" AutoPostBack="true" OnSelectedIndexChanged="OnSelectedIndexChanged">
<asp:ListItem Text="test" />
<asp:ListItem Text="test1" />
<asp:ListItem Text="test2" />
</asp:RadioButtonList>
<hr />
<asp:GridView runat="server" ID="gvDetails"></asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
private DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Columns.Add("Sal");
dt.Columns.Add("State");
dt.Rows.Add(1, "aa", 1, "test");
dt.Rows.Add(2, "bb", 1, "test2");
dt.Rows.Add(3, "cc", 1, "test");
dt.Rows.Add(4, "dd", 1, "test1");
dt.Rows.Add(5, "ee", 1, "test1");
dt.Rows.Add(6, "ff", 1, "test");
dt.Rows.Add(7, "gg", 1, "test1");
dt.Rows.Add(8, "hh", 1, "test2");
return dt;
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = GetData();
DataRow[] selected = dt.Select("State='" + rblStates.SelectedItem.Text + "'").OrderBy(x=>x["ID"]).ToArray();
DataRow[] unSelected = dt.Select("State<>'" + rblStates.SelectedItem.Text + "'").OrderBy(x => x["ID"]).ToArray();
if (selected.Length > 0 && unSelected.Length > 0)
{
DataTable result = selected.CopyToDataTable();
result.Merge(unSelected.CopyToDataTable());
gvDetails.DataSource = result;
gvDetails.DataBind();
}
}
VB.Net
Private Function GetData() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.Add("ID")
dt.Columns.Add("Name")
dt.Columns.Add("Sal")
dt.Columns.Add("State")
dt.Rows.Add(1, "aa", 1, "test")
dt.Rows.Add(2, "bb", 1, "test2")
dt.Rows.Add(3, "cc", 1, "test")
dt.Rows.Add(4, "dd", 1, "test1")
dt.Rows.Add(5, "ee", 1, "test1")
dt.Rows.Add(6, "ff", 1, "test")
dt.Rows.Add(7, "gg", 1, "test1")
dt.Rows.Add(8, "hh", 1, "test2")
Return dt
End Function
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = GetData()
Dim selected As DataRow() = dt.Select("State='" & rblStates.SelectedItem.Text & "'").OrderBy(Function(x) x("ID")).ToArray()
Dim unSelected As DataRow() = dt.Select("State<>'" & rblStates.SelectedItem.Text & "'").OrderBy(Function(x) x("ID")).ToArray()
If selected.Length > 0 AndAlso unSelected.Length > 0 Then
Dim result As DataTable = selected.CopyToDataTable()
result.Merge(unSelected.CopyToDataTable())
gvDetails.DataSource = result
gvDetails.DataBind()
End If
End Sub
Screenshot