Hi ramco1917,
You need to make use of Linq query.
Please refer below sample.
Note: For this sample i have used temporary DataTable. For more details refer Dynamically create DataTable and bind to GridView in ASP.Net.
HTML
Country:
<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Search" OnClick="Search" />
<hr />
<asp:GridView ID="gvCustomers" runat="server"></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 = this.GetData();
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
protected void Search(object sender, EventArgs e)
{
string country = txtCountry.Text;
DataTable dt = this.GetData();
if (!string.IsNullOrEmpty(country))
{
var customers = from customer in dt.AsEnumerable()
where customer.Field<string>("Country") == country
select new
{
CustomerId = customer.Field<string>("CustomerId"),
Name = customer.Field<string>("Name"),
Country = customer.Field<string>("Country")
};
gvCustomers.DataSource = customers;
gvCustomers.DataBind();
}
else
{
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
private DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("CustomerId"),
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");
return dt;
}
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 = Me.GetData()
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End If
End Sub
Protected Sub Search(ByVal sender As Object, ByVal e As EventArgs)
Dim country As String = txtCountry.Text
Dim dt As DataTable = Me.GetData()
If Not String.IsNullOrEmpty(country) Then
Dim customers = From customer In dt.AsEnumerable() Where customer.Field(Of String)("Country") = country
Select New With {Key _
.CustomerId = customer.Field(Of String)("CustomerId"), Key _
.Name = customer.Field(Of String)("Name"), Key _
.Country = customer.Field(Of String)("Country")
}
gvCustomers.DataSource = customers
gvCustomers.DataBind()
Else
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End If
End Sub
Private Function GetData() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("CustomerId"),
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")
Return dt
End Function
Screenshot