簡體   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