繁体   English   中英

如何在Datagridview上应用过滤器?

[英]How to apply Filter on datagridview?

美好的一天。
我是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)

谁能帮助我解决这个问题并向我解释我遇到了什么问题? 谢谢。
对不起,我的英语不好:)

您的情况完全错误。 这就是你的做法

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引起order by 您可能没有“类型”列

另一个问题是txtsidtxtsprice这是字符串吗? 如果不是,则需要根据情况删除单引号。

就您而言,筛选器可能比我在这里看到的要容易...

  ' Create a DataView 
    Dim dv As New DataView(DsSales1.Tables(0))

    ' Filter by an expression. 
    dv.RowFilter = zCondition

另外,在检查中您还可以执行以下操作: zCondition &=添加字符串...另一方面**,**在添加其他字符串时**,**在条件满足后摆脱**,** ,请使用**AND**

您的假设也可以归结为...

    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

这就是我所做的:)
对不起,我忘了提到我也将**,**更改为** 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM