vb.net DataGridView 基于值过滤行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24924427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:53:35  来源:igfitidea点击:

DataGridView filter rows based on a value

vb.netdatagridviewvisual-studio-2013visual-studio-express

提问by melmatvar

I am working on a windows form in vs2013.I am trying to display records based on a variable on to DataGridView when my form loads. I have tried a whole variety of code based on some google research.

我正在 vs2013 中处理 Windows 窗体。我试图在我的窗体加载时将基于变量的记录显示到 DataGridView 上。我已经根据谷歌的一些研究尝试了各种各样的代码。

I am connect form data to an access database.

我将表单数据连接到访问数据库。

Here is the code:

这是代码:

Dim maxrecords As Integer
Dim firstrecord As Integer
Dim currentrecord As Integer
Dim deletedrecordnum As Integer
Dim con As New OleDb.OleDbConnection
Dim del As New OleDb.OleDbConnection
Dim dbprov As String
Dim dbsource As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim dsDelete As New DataSet
Dim daDelete As OleDb.OleDbDataAdapter
Dim sql As String
Dim sqlDelete As String
Dim currentday As String

'This is the first thing that happens before the form loads up on the screen
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    dbprov = "Provider = Microsoft.ACE.OLEDB.12.0;"
    dbsource = "Data Source = C:\VB_Projects\database1.accdb"
    sql = "SELECT * FROM T_Cutdata"
    sqlDelete = "SELECT * FROM T_DeletedData"
    con.ConnectionString = dbprov & dbsource
    con.Open()
    MsgBox("Open")
    da = New OleDb.OleDbDataAdapter(sql, con)
    da.Fill(ds, "lawncutdata")
    daDelete = New OleDb.OleDbDataAdapter(sqlDelete, con)
    daDelete.Fill(dsDelete, "lawncutdata")
    con.Close()
    MsgBox("Closed")

    If IsDBNull(ds.Tables("lawncutdata").Rows(0).Item(3)) Then
        CheckBox1.Checked = False
    Else
        CheckBox1.Checked = ds.Tables("lawncutdata").Rows(0).Item(3)
    End If

    If IsDBNull(ds.Tables("lawncutdata").Rows(0).Item(5)) Then
        TextBox5.Text = ""
    Else
        TextBox5.Text = ds.Tables("lawncutdata").Rows(0).Item(5)
    End If
    maxrecords = ds.Tables("lawncutdata").Rows.Count - 1
    currentrecord = 0
    firstrecord = 0
    currentday = "Monday"
    DataGridView1.DataSource = ds.Tables("lawncutdata")
    For i As Integer = firstrecord To maxrecords
        If DataGridView1.Rows(i).Cells(1).ToString = currentday Then
        DataGridView1.DataSource = ds.Tables("lawncutdata").Rows(3)
    EndIf
Next
    ds.Tables("lawncutdata").DefaultView.RowFilter.Contains("Monday")
End Sub

PS: What is the best way to store daily data? I will be inputting data on a daily basis. Should I create a table for monday, tuesday, etc?

PS:日常数据的最佳存储方式是什么?我将每天输入数据。我应该为星期一、星期二等创建一张桌子吗?

I am having such a hard time with this. Any help is truly appreciated.

我在这方面很难受。任何帮助都非常感谢。

Thanks melmatvar

谢谢 melmatvar

采纳答案by jmcilhinney

Get rid of your loop. Set the DefaultView.RowFilterof the DataTableand then assign the DataTableto the DataSourceof the grid. That's it: filter and bind. To filter, you actually have to assign a Stringto the RowFilter, e.g.

摆脱你的循环。设置DefaultView.RowFilterDataTable,然后分配DataTableDataSource电网。就是这样:过滤和绑定。要过滤,您实际上必须将 a 分配StringRowFilter,例如

myDataTable.DefaultView.RowFilter = filterString

That said, if you only want one day's data to display then you should only retrieve one day's data from the database. If you want to be able to change the filter without requerying then get all the data.

也就是说,如果您只想显示一天的数据,那么您应该只从数据库中检索一天的数据。如果您希望能够在不重新查询的情况下更改过滤器,请获取所有数据。

No, you should absolutely not create multiple tables. Each table represents a single entity. The entity is not different because it relates to a different day. One table with a column for the day/date/whatever is the correct option.

不,您绝对不应该创建多个表。每个表代表一个实体。该实体没有什么不同,因为它与不同的日子有关。一张表格,其中包含日期/日期/任何正确选项的列。