简体   繁体   中英

vb.net datagridview filtering colum not running

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

at the top i declared:

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

Here is the rest of my code:

    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

the filtering colum not running because the filtered column [barkod] not exist in the datatable; you make in query select barkod as 'Баркод' .

if you want to show the header DatagridView with a specific name you can create query without (as) like this :

  Query = "select barkod, naziv, kupovna, prodazna, opis, profit, 
    proizvoditel, ddv, kolicina, makpr from baza.artikli"

and change name of header column in DataGridView like this :

DataGridView1.Columns["barkod"].HeaderText = "Баркод"
DataGridView1.Columns["naziv"].HeaderText = "Назив"
DataGridView1.Columns["kupovna"].HeaderText = "Куповна"
//...

And now you can apply your filter :

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

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

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 .

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

Basic Query and Actors

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. 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.

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

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

Built-in 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: 在此处输入图片说明

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

在此处输入图片说明 The Alias columns from the SQL are used ( A , C , Баркод )
在此处输入图片说明 Only rows containing "erm" for Баркод are shown

Escape Illegal Characters

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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