行不能以编程方式添加
问题描述:
我试图填充DataGrid视图与内容自动对焦,我用下面的代码的文本文件:行不能以编程方式添加
Private Sub Button15_Click(sender As Object, e As EventArgs) _
Handles Button15.Click
'strPath is the location of text file
Dim lines = (From line In IO.File.ReadAllLines(strPath)
Select line.Split(CChar(vbTab))).ToArray
For x As Integer = 0 To lines(0).GetUpperBound(0)
dgQuotation.Columns.Add(lines(0)(x), lines(0)(x))
Next
For x As Integer = 1 To lines.GetUpperBound(0)
dgQuotation.Rows.Add(lines(x))
Next
End Sub
但我每次运行该程序,我得到以下运行时间时间错误:
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
请帮忙,我是VB新手。提前致谢。
答
这意味着您需要使用数据绑定或手动添加行 - 选择一个,而不是两个。
答
而不是两个for循环,您可以只设置dgQuotation.DataSource = lines ...如果网格是数据绑定的,则以编程方式向网格添加值(如您在代码中执行的操作)是不可能的(=> DataSource是设置)
您必须在datagrid视图的数据源中添加新行。 – Jade