Preamble:
Having not worked with WinForms for a long while and also never before in VB I am trying to add a DataGridView
to an application written in VB which will display a grid of data from a DataTable
.
I followed the documentation here, here and here and in a simple test example I have the code
Public Class Form1
Private count As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
count = count + 1
Dim table As DataTable = BindingSource2.DataSource
Dim row As DataRow
row = table.NewRow()
row("Col1") = "foo" + count.ToString()
row("Col2") = "bar" + count.ToString()
table.Rows.Add(row) 'throws System.InvalidOperationException here
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'BindingSource2.DataSource = New DataTable()
'Dim table As DataTable = BindingSource2.DataSource
Dim table As New DataTable
Dim column1 As DataColumn = New DataColumn()
column1.ColumnName = "Col1"
column1.Caption = column1.ColumnName
column1.DataType = System.Type.GetType("System.String")
table.Columns.Add(column1)
Dim column2 As DataColumn = New DataColumn()
column2.ColumnName = "Col2"
column2.Caption = column2.ColumnName
column2.DataType = System.Type.GetType("System.String")
table.Columns.Add(column2)
'Dim keys(0) As DataColumn
'keys(0) = column1
'table.PrimaryKey = keys
' first row
Dim row As DataRow = table.NewRow()
row("Col1") = "beep"
row("Col2") = "boop"
table.Rows.Add(row)
BindingSource2.DataSource = table
End Sub
End Class
The code gets through Form1_Load
okay however the entries added there are not shown in the DataGridView
. Then when Timer1_Tick
is called it throws a System.InvalidOperationException
exception at the line indicated above. I cannot see what I am doing wrong based on the examples given in the docs.
Question: Can anybody help please, 开发者_开发百科with (a) why does the DataGridView
not reflect the added data at the end of Form1_Load
and (b) why is adding a row causing an exception?
P.s. I've checked the debug and at the table.Rows.Add(row)
table
contains the correct information as does row
.
Edit: The BindingSource was added and connected to the DataGridView using the designer so the code for it is shown instead in From1.Designer.vb which I haven't shown here.
The solution (at least in my case here) was that the DataGridView.AutoGenerateColumns
is not shown in the design panel and is by default set to False
. I simply added the line
DataGridView1.AutoGenerateColumns = True
to my code (in Form1_Load
) and it worked perfectly. I found the solution on a forum but now cannot find the link. I'll add it if I find it.
I don't know what to say. I used your code exactly and there's no exception happening. It works just fine. What I guess could be the problem is maybe the Timer1.Interval could be too low a value in conjunction to how fast the machine you are running the code on is. What if you increase the interval?
As for the data not being displayed ... add two columns to the DataGridView set the DataPropertyName property of each DataGridView column to match with each of the names of the table's column (in the Edit Colums... dialog). Then you'll get your data displayed.
精彩评论