数据视图的RowFilter [英] Dataview Rowfilter
问题描述
我有一个文本框和一个按钮。我所试图做的是过滤一个gridview与已搜索的数据。对于这两个字段IM搜索是CustomerID和客户名称。
I have a textbox and a button. What i am attempting to do is filter a gridview with the data that has been searched for. The two fields im searching for are customerID and CustomerName.
我的code:
Dim GetAllCusts As New BusinessLayer.Customer
Dim dt As DataTable = GetAllCusts.GetCustomers
Dim dv As New DataView(dt)
dv.RowFilter = String.Format("CustomerID='{0}' Or CustomerName='{0}'", SearchTextbox.Text)
gridview1.DataSource = dv
gridview1.DataBind()
当一个客户ID一切正常搜索。当客户名称搜索我得到无法在System.Int32和System.String执行'='操作。
When searching for a customer id everything works. When searching for a customer name i get "Cannot perform '=' operation on System.Int32 and System.String.".
我想转换(客户名称,System.String),但是这却决心剪掉我认为这是导致我从最初的问题走,我就开始想身边。
I tried to CONVERT(CustomerName, System.String) but that didnt resolve however i think this was leading me away from the initial problem as i started to guess around.
我想有一个文本框来搜索一个客户ID和客户名称。什么是错的的RowFilter和如何解套?
I would like to have one textbox to search for a customer id and customer name. What is wrong with the RowFilter and how could it be resolved?
感谢
推荐答案
您有一个概念性的问题。或者你搜索一个号码或您搜索的字符串。结果
我想你应该改变你的code这样
You have a conceptual problem. Or you search for a number or you search for a string.
I think you should change your code in this way
Dim custID as Integer
if Int32.TryParse(SearchTextbox.Text, custID) Then
' CustomerID is a numeric field - no need to use single quotes'
dv.RowFilter = String.Format("CustomerID={0}", custID)
else
' CustomerName is a string. Use single quotes and double the '
' possible single quotes inside the search box'
dv.RowFilter = String.Format("CustomerName='{0}'", SearchTextbox.Text.Replace("'", "''")
End If
gridview1.DataSource = dv
这篇关于数据视图的RowFilter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!