美好的一天!
使用datagridview重新加载表单以加载所有数据时遇到问题。
错误:
CODE:
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource
Private Sub frmHistory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Try
CallDatabase()
'Dim da As OleDbDataAdapter
'Dim ds As DataSet
'Dim tables As DataTableCollection
'Dim source1 As New BindingSource
cnnOLEDB.ConnectionString = strConnectionString
cnnOLEDB.Open()
ds = New DataSet
tables = ds.Tables
da = New OleDbDataAdapter("Select * from [tblHistory]", cnnOLEDB) 'Change items to your database name
da.Fill(ds, "tblHistory") 'Change items to your database name
Dim view As New DataView(tables(0))
source1.DataSource = view
gridHistory.DataSource = view
gridHistory.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders
gridHistory.BackgroundColor = System.Drawing.SystemColors.Control
gridHistory.Columns(0).Width = 40
gridHistory.Columns(0).HeaderText = "No."
gridHistory.Columns(1).Width = 80
gridHistory.Columns(1).HeaderText = "Amount"
gridHistory.Columns(2).Width = 70
gridHistory.Columns(2).HeaderText = "Hours"
gridHistory.Columns(3).Width = 70
gridHistory.Columns(3).HeaderText = "Minutes"
gridHistory.Columns(4).Width = 70
gridHistory.Columns(4).HeaderText = "Pages"
gridHistory.Columns(5).Width = 100
gridHistory.Columns(5).HeaderText = "Date"
cnnOLEDB.Close()
'Catch ex As Exception
' Dim errMsg As String =
' "Error: {0} " & vbLf & "Please contact your Cyber Administrator."
' errMsg = String.Format(errMsg, ex.Message)
' MessageBox.Show(errMsg, "Application Error", MessageBoxButtons.OK)
' Me.Hide()
'End Try
End Sub
每次重新加载表单时,这行代码都会出错:
gridHistory.Columns(0).Width = 40
依旧......
如果删除该代码,它将起作用但该代码的目的是自定义我的datagridview,因为它占用了大量的屏幕空间。
在我的tblHistory上这些是字段
ID,金额,小时,分钟,页数,记录日期
如果我添加上面的代码,那将是ooutput:
否,金额,小时数,分钟数,页数,日期和所有列宽将根据每个列标题上的单元格宽度进行划分。
输出:
在我评论代码之前(gridHistory.Columns(0).Width = 40)
我评论代码后
看到区别?