how to search in gridview values using textbox
VB.NET in ASP.NET
This example demonstrates how to search/query gridview using textbox .
Step 1) Add GridView and bind it to List of Customer History.
<div style="width:600px;">
<div style="float:right;"><font color="green;"><b>Search String:</b></font>
<asp:TextBox ID="txtQuery" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" /></div>
<div style="float:right;">
<asp:GridView Caption="<h1 style='color:red;text-align:left;'>GridView Search using Textbox</h1>" CaptionAlign="Left" ID="GridView1" runat="server" Width="568px" EmptyDataText="No data Found"></asp:GridView>
</div>
</div>
add a class called CustOrderHistResult.vb
Public Class CustOrderHistResult
Public WriteOnly Property ProductName() As String
Set (ByVal Value As String)
}
End Set
End Property
Public ReadOnly Property Total() As Integer
Get
}
End Get
End Property
End Class
Here is the OUTPUT
Figure 1: All data
Figure 2: searching for value 6
Figure 3: searching for "spread" as shown below
Figure 4: Pass some junk value , it returns Search result is nothing..
Complete Source Code
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace WebApplication1
Public partial Class GridView_and_Search_EnityDataSource
Inherits System.Web.UI.Page
'we need 2 lists
'1 for actual data
'2 for search data
Dim listCustHist As List<CustomerOrdersHistory> = Nothing
Dim searchList As List<CustomerOrdersHistory> = New List<CustomerOrdersHistory>()
protected void Page_Load(Object sender, EventArgs e)
{
'System.Data.Linq.Table < CustOrderHistResult> custOrder = context.GetTable<CustOrderHistResult>();
if (Not IsPostBack)
{
'Build some list with sample data
Build_CustOrderHistResult_Data()
'first time store it in Session Object
Session("custHistory") = listCustHist
}
GridView1.DataSource = listCustHist
GridView1.DataBind()
}
protected void btnSearch_Click(Object sender, EventArgs e)
{
Dim searchString As String = txtQuery.Text
if (Session("custHistory") <> Nothing)
listCustHist = CType(Session("custHistory"), List<CustomerOrdersHistory>)
if (listCustHist <> Nothing And searchString <> String.Empty)
{
Dim chList As CustomerOrdersHistory
For Each chList In listCustHist
Dim m As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(chList.ProductName,searchString)
if (m.Success)
{
searchList.Add(chList)
}
else
{
m = System.Text.RegularExpressions.Regex.Match(chList.Total.ToString(), searchString)
Dim searchList.AddCType(As if(m.Success), chList)
}
Next
}
GridView1.EmptyDataText = "Search result is Nothing..."
GridView1.DataSource = (searchString= IIf(String.Empty), listCustHist, searchList) 'if query string is empty display all records
'else display search results
'else no data found for search will be displayed.
GridView1.DataBind()
}
Public Class CustomerOrdersHistory
Public Property ProductName() As String
End Property
Public Property Total() As Integer
End Property
End Class
void Build_CustOrderHistResult_Data()
{
if (listCustHist = Nothing) listCustHist = New List<CustomerOrdersHistory>()
listCustHist.Add(New CustomerOrdersHistory
{
ProductName = "Aniseed Syrup ", Total = 6
}
)
listCustHist.Add(New CustomerOrdersHistory
{
ProductName = "Chartreuse verte", Total = 21
}
)
listCustHist.Add(New CustomerOrdersHistory
{
ProductName = "Escargots de Bourgogne", Total = 40
}
)
listCustHist.Add(New CustomerOrdersHistory
{
ProductName = "Flotemysost", Total=20
}
)
listCustHist.Add(New CustomerOrdersHistory
{
ProductName = "Grandma's Boysenberry Spread", Total = 16
}
)
listCustHist.Add(New CustomerOrdersHistory
{
ProductName = "Lakkalikööri", Total=15
}
)
listCustHist.Add(New CustomerOrdersHistory
{
ProductName = "Original Frankfurter grüne Soße", Total=2
}
)
listCustHist.Add(New CustomerOrdersHistory
{
ProductName = "Raclette Courdavault", Total=15
}
)
listCustHist.Add(New CustomerOrdersHistory
{
ProductName = "Rössle Sauerkraut", Total=17
}
)
listCustHist.Add(New CustomerOrdersHistory
{
ProductName = "Spegesild", Total=2
}
)
listCustHist.Add(New CustomerOrdersHistory
{
ProductName = "Vegie-spread", Total=20
}
)
}
End Class
End Namespace
This example filters grid view data using textbox value as search string, used regular expressions to filter data in the list
No comments:
Post a Comment