我正在使用vs2013中的Windows窗体。我正在尝试在加载表单时根据DataGridView上的变量显示记录。我根据一些谷歌研究尝试了各种代码。
我将表单数据连接到访问数据库。
以下是代码:
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:存储每日数据的最佳方式是什么?我将每天输入数据。我应该为星期一,星期二等创建一个表吗?
我对此感到非常困难。真的很感激任何帮助。
由于 melmatvar
答案 0 :(得分:0)
摆脱你的循环。设置DefaultView.RowFilter
的{{1}},然后将DataTable
分配给网格的DataTable
。这就是它:过滤和绑定。要进行过滤,您实际上必须为DataSource
分配String
,例如
RowFilter
也就是说,如果您只想显示一天的数据,那么您应该只从数据库中检索一天的数据。如果您希望能够在不重新查询的情况下更改过滤器,则获取所有数据。
不,你绝对不应该创建多个表。每个表代表一个实体。实体没有区别,因为它与不同的一天有关。一个表格中包含日期/日期/列的正确选项。