vb.net datagridview 过滤列未运行 [英] vb.net datagridview filtering colum not running

查看:45
本文介绍了vb.net datagridview 过滤列未运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开发了一个 pos,但数据搜索出现错误.这是一些代码:

Recently I developed a pos but the data search is giving errors. Here is some code:

 Dim DV As New DataView(dbDataSet)
    DV.RowFilter = String.Format("[barkod] Like '%" & TextBoxPrebBarkod.Text & "%'")
    DataGridView1.DataSource = DV

在顶部我声明:

    Dim dbDataSet As New DataTable
    konekcija = New MySqlConnection
    konekcija.ConnectionString = "server=localhost;userid=root;password=root;database=baza"

这是我的其余代码:

    Dim SDA As New MySqlDataAdapter

    Dim bSource As New BindingSource

    Try
        konekcija.Open()
        Dim Query As String
        Query = "select barkod as 'Баркод', naziv as 'Назив', kupovna as 
'Куповна', prodazna as 'Продажна', opis as 'Опис', profit as 'Профит', 
proizvoditel as 'Производител', ddv as 'ДДВ', kolicina as 'Количина' 
, makpr as 'Македонски производ' from baza.artikli"
                    comm = New MySqlCommand(Query, konekcija)
                    SDA.SelectCommand = comm
                    SDA.Fill(dbDataSet)
                    bSource.DataSource = dbDataSet
                    DataGridView1.DataSource = bSource
                    SDA.Update(dbDataSet)
                    konekcija.Close()
                Catch ex As Exception

                End Try

推荐答案

有多种方法可以过滤查询结果,但是给出错误对于问题所在并不是很有帮助.

There are several ways to filter a query result, but is giving errors is not very helpful as to what the problem is.

首先,当您为列添加别名(barkod as 'Баркод')时,别名添加到DataTable中作为该名称;如果不使用别名 (As Foo),它就没有意义.如果 AutoGenerateColumns 对于 DataGridView 为 true,则这些列名称也将用于 HeaderText.

First, when you Alias a column (barkod as 'Баркод') the Alias names are added to the DataTable as that name; there would be no point to an Alias (As Foo) it if it didn't use them. If AutoGenerateColumns is true for the DataGridView, those columns names will also be used for the HeaderText.

我不知道为什么 OP 代码使用 BindingSourceDataTable 确实是你所需要的,所以我省略了它.

I am not sure why the OP code is using a BindingSource, the DataTable is really all you need, so I omitted it.

Private dtSample As DataTable       ' form level object
...

Dim sql = "SELECT Name AS A, Fish AS Баркод, Bird As C, Color From Sample"

dtSample = New DataTable
Using dbcon As New MySqlConnection(MySQLConnStr)
    Using cmd As New MySqlCommand(sql, dbcon)

        dbcon.Open()
        dtSample.Load(cmd.ExecuteReader())

        ' prove that the DT column name is that of the SQL alias:
        Console.WriteLine(dtSample.Columns(1).ColumnName)   ' == "Баркод"
    End Using
End Using

  1. 这使用表单级别DataTable.根据将实施的操作,这可以防止必须强制转换 DGV 的 DataSource 以便在其他地方使用它.
  2. 注意使用 Using 来关闭和处理 DBConnection 和 DBCommand 对象.在幕后,这些分配需要释放的资源.
  1. This uses a form level DataTable. Depending on what operations will be implemented, this prevents having to cast the DGV's DataSource in order to work with it elsewhere.
  2. Note the use of Using to close and dispose of the DBConnection and DBCommand objects. Under the hood, these allocate resources which need to be released.

显式数据视图

根据代码的作用,持久的 DataView 可能很有用.这将用于过滤:

Explicit DataView

Depending on what the code does, a persistent DataView can be useful. This will be used for the filtering:

Private dvSample As DataView         ' declared with dtSample
...
{query code from above}
dvSample = New DataView(dtSample)

dvSample.RowFilter = String.Format("Баркод LIKE '%{0}%'", "erm")
dgv2.DataSource = dvSample

我不喜欢使用 LIKE,但您可以使用它.

I am not fond of using LIKE, but there you have it.

这有点矫枉过正,因为 DataTable 有一个内置的 DataView 将使用:

That is slightly overkill because the DataTable has a built in DataView which this one will use:

dtSample.DefaultView.RowFilter = String.Format("Баркод LIKE '%{0}%'", "erm")
dgv2.DataSource = dtSample

无论哪种方法,结果都是一样的:

With either method, the results are the same:

使用 BindingSource.Filter 会得到相同的结果.

You'd get the same results using the BindingSource.Filter.

使用 SQL 中的别名列 (A, C, Баркод>)
仅包含erm"的行Баркод 显示

The Alias columns from the SQL are used (A, C, Баркод)
Only rows containing "erm" for Баркод are shown

转义非法字符

同样,不清楚要修复的内容它给出了错误,但可能TextBoxPrebBarkod.Text 中的任何内容都包含非法字符.O'BrianCarol's CookiesD'Angelo 之类的名字会因为打勾/撇号而导致问题.这些可以通过用 2 替换一个刻度来转义:

Again, it is not clear what is to be fixed from it gives errors but possibly whatever is in TextBoxPrebBarkod.Text contains illegal characters. Names like O'Brian, Carol's Cookies or D'Angelo will cause problems because of the tick/apostrophe. These can be escaped by replacing one tick with 2:

Dim filterText = "D'Artagnan"    ' e.g. user input like TextBoxPrebBarkod.Text
filterText = filterText.Replace("'", "''")     ' swap 2 ticks for 1
dtSample.DefaultView.RowFilter = String.Format("Баркод LIKE '%{0}%'", filterText)

当然,总是有可能在 Баркод 列中不存在该 TextBox 中的任何内容.

Of course, there is always the chance that whatever is in that TextBox doesnt exist in the Баркод column.

这篇关于vb.net datagridview 过滤列未运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆