vb.net 数据视图行过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15809578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:06:39  来源:igfitidea点击:

Dataview Rowfilter

c#asp.netvb.netgridviewdataview

提问by Computer

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.

我有一个文本框和一个按钮。我试图做的是使用已搜索的数据过滤 gridview。我正在搜索的两个字段是 customerID 和 CustomerName。

My 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()

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.".

搜索客户 ID 时一切正常。搜索客户名称时,我收到“无法在 System.Int32 和 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.

我试图 CONVERT(CustomerName, System.String) 但这并没有解决但是我认为这让我远离了最初的问题,因为我开始猜测。

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?

我想要一个文本框来搜索客户 ID 和客户名称。RowFilter 有什么问题,如何解决?

Thanks

谢谢

回答by Steve

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

回答by LightRay

I guess CutomerId is a integer field and when you try to search for a Name which is a string field you are trying to search an integer field with a string value.

我猜 CutomerId 是一个整数字段,当您尝试搜索作为字符串字段的 Name 时,您正在尝试搜索具有字符串值的整数字段。