简体   繁体   English

如何在Datagridview上应用过滤器?

[英]How to apply Filter on datagridview?

Good day. 美好的一天。
I am a beginner in vb.net and new in stackoverflow , I am encountering a problem in Filter method... 我是vb.net的初学者,还是stackoverflow的新手,我在Filter方法中遇到问题...
Below is my code: 下面是我的代码:

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

When I execute and test the code, it's give me a error Cannot find column type. 当我执行和测试代码时,它给我一个错误, Cannot find column type. ... ...
It seems this line is having error: 似乎此行有错误:

dv = New DataView(DsSales1.Tables(0), zCondition, "type Desc", DataViewRowState.CurrentRows)

Can anyone help me solve this problem and explain to me what's the problem I am having? 谁能帮助我解决这个问题并向我解释我遇到了什么问题? Thank you. 谢谢。
And sorry for my bad English :) 对不起,我的英语不好:)

Your condition is totally wrong. 您的情况完全错误。 This is how you do it 这就是你的做法

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)

Your error most likely caused by your order by . 您的错误很可能是由的order by引起order by You probably don't have column "type" 您可能没有“类型”列

Another question to you is txtsid , txtsprice - are this strings? 另一个问题是txtsidtxtsprice这是字符串吗? If they are not - you need to remove single quotes in your condition. 如果不是,则需要根据情况删除单引号。

In your case filter can be easier than what I am seeing here... 就您而言,筛选器可能比我在这里看到的要容易...

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

    ' Filter by an expression. 
    dv.RowFilter = zCondition

Also in your checks you can do: zCondition &= to add strings... On another note get rid of **,** after your condition when adding on another string, use **AND** 另外,在检查中您还可以执行以下操作: zCondition &=添加字符串...另一方面**,**在添加其他字符串时**,**在条件满足后摆脱**,** ,请使用**AND**

Your if's can come down to this as well... 您的假设也可以归结为...

    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 MrCodexer

This is what I have done:) 这就是我所做的:)
And sorry for I forgot to mention that I had also change the **,** to ** AND ** 对不起,我忘了提到我也将**,**更改为** 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