美好的一天
我是vb.net的初学者和stackoverflow中的新手,我在Filter
方法中遇到问题...
以下是我的代码:
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim zCondition As String = ""
If txtrid.Text.Length > 0 Then zCondition = zCondition & "receiptID='" & txtrid.Text & "',"
If txtsid.Text.Length > 0 Then zCondition = zCondition & "stkID='" & txtsid.Text & "',"
If txtsname.Text.Length > 0 Then zCondition = zCondition & "stkName='" & txtsname.Text & "',"
If txtsprice.Text.Length > 0 Then zCondition = zCondition & "stkPrice='" & txtsprice.Text & "',"
If txtsquantity.Text.Length > 0 Then zCondition = zCondition & "quantity='" & txtsquantity.Text & "',"
If txtdate.Text.Length > 0 Then zCondition = zCondition & "dateTime='" & txtdate.Text & "',"
If txtcid.Text.Length > 0 Then zCondition = zCondition & "custID='" & txtcid.Text & "',"
zCondition = zCondition.Substring(0, zCondition.Length - 1)
Dim dv As DataView
dv = New DataView(DsSales1.Tables(0), zCondition, "type Desc", DataViewRowState.CurrentRows)
DataGridView1.DataSource = dv
End Sub
当我执行并测试代码时,它会给我一个错误Cannot find column type.
...
这条线似乎有错误:
dv = New DataView(DsSales1.Tables(0), zCondition, "type Desc", DataViewRowState.CurrentRows)
任何人都可以帮我解决这个问题并向我解释我遇到的问题是什么?谢谢。
抱歉我的英语不好:)
答案 0 :(得分:1)
你的情况完全错了。你就是这样做的
Dim cond As New StringBuilder(1000)
cond.Append("1 = 1") ' now, you will never add extra 'AND', 'OR' etc. 1 is always 1
' if any text box below is missing data it will not hurt anything using this technique
' NOTE spaces in front of 'AND'
If txtrid.Text.Length > 0 Then cond.Append(" AND receiptID='" & txtrid.Text & "'") ' removed commas
If txtsid.Text.Length > 0 Then cond.Append(" AND stkID='" & txtsid.Text & "'")
If txtsname.Text.Length > 0 Then cond.Append(" AND stkName='" & txtsname.Text & "'")
If txtsprice.Text.Length > 0 Then cond.Append(" AND stkPrice='" & txtsprice.Text & "'")
If txtsquantity.Text.Length > 0 Then cond.Append(" AND quantity='" & txtsquantity.Text & "'")
If txtdate.Text.Length > 0 Then cond.Append(" AND dateTime='" & txtdate.Text & "'")
If txtcid.Text.Length > 0 Then cond.Append(" AND custID='" & txtcid.Text & "'")
Dim dv As New DataView(DsSales1.Tables(0), cond.ToString(), "COL_NAME Desc", DataViewRowState.CurrentRows)
您的错误很可能是由order by
引起的。您可能没有列“类型”
另一个问题是txtsid
,txtsprice
- 这是字符串吗?如果不是 - 你需要在你的条件下删除单引号。
答案 1 :(得分:0)
在你的情况下,过滤器可能比我在这里看到的更容易......
' Create a DataView
Dim dv As New DataView(DsSales1.Tables(0))
' Filter by an expression.
dv.RowFilter = zCondition
同样在您的支票中,您可以执行:zCondition &=
添加字符串...在另一个注意事项中,在添加其他字符串后,删除**,**
后,使用**AND**
你的if也可以归结为这个......
If txtrid.Text.Length > 0 Then zCondition &= "receiptID='" & txtrid.Text & "'"
If txtsid.Text.Length > 0 AndAlso txtrid.Text.Length > 0 Then zCondition &= "AND stkID='" & txtsid.Text & "'" Else zCondition &= "stkID='" & txtsid.Text & "'"
If txtsname.Text.Length > 0 AndAlso txtsid.Text.Length > 0 Then zCondition &= "AND stkName='" & txtsname.Text & "'" Else zCondition &= "stkName='" & txtsname.Text & "'"
MrCodexer
答案 2 :(得分:0)
这就是我所做的:)
抱歉,我忘了提及我也将**,**
更改为** AND **
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim zCondition As String = ""
If txtrid.Text.Length > 0 Then zCondition = zCondition & "receiptID='" & txtrid.Text & "' AND "
If txtsid.Text.Length > 0 Then zCondition = zCondition & "stkID='" & txtsid.Text & "' AND "
If txtsname.Text.Length > 0 Then zCondition = zCondition & "stkName='" & txtsname.Text & "' AND "
If txtsprice.Text.Length > 0 Then zCondition = zCondition & "stkPrice='" & txtsprice.Text & "' AND "
If txtsquantity.Text.Length > 0 Then zCondition = zCondition & "quantity='" & txtsquantity.Text & "' AND "
If txtdate.Text.Length > 0 Then zCondition = zCondition & "dateTime='" & txtdate.Text & "' AND "
If txtcid.Text.Length > 0 Then zCondition = zCondition & "custID='" & txtcid.Text & "' AND "
zCondition = zCondition.Substring(0, zCondition.Length - 5)
Dim dv As DataView
dv = New DataView(DsSales1.Tables(0), zCondition, "receiptID Desc", DataViewRowState.CurrentRows)
DataGridView1.DataSource = dv
End Sub