Dataview Rowfilter

时间:2013-04-04 11:13:57

标签: c# asp.net vb.net gridview dataview

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

我的代码:

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上获得“无法执行'='操作。”。

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

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

由于

2 个答案:

答案 0 :(得分:4)

你有一个概念性的问题。或者您搜索一个数字或搜索字符串 我认为你应该以这种方式改变你的代码

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

答案 1 :(得分:0)

我猜CutomerId是一个整数字段,当你尝试搜索一个字符串字段的Name时,你试图用字符串值搜索一个整数字段。