2014-01-15 71 views
0

我試圖填充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新手。提前致謝。

+0

您必須在datagrid視圖的數據源中添加新行。 – Jade

回答

0

這意味着您需要使用數據綁定或手動添加行 - 選擇一個,而不是兩個。

0

而不是兩個for循環,您可以只設置dgQuotation.DataSource = lines ...如果網格是數據綁定的,則以編程方式向網格添加值(如您在代碼中執行的操作)是不可能的(=> DataSource是設置)

相關問題