无法设置数据绑定DataGridView添加列的值

时间:2016-07-29 20:54:01

标签: vb.net winforms

我有一个绑定在代码后面的DataGridView,我添加了一个列,然后我需要在同一行的基于列的列中插入数据。

 Dim db2 As New MyDBDataContext
 Dim r = (From p In db2.EventConfigs)
 GridTemplate.DataSource = r

我在网格中添加了一列

    Dim col As New DataGridViewTextBoxColumn
    col.ValueType = GetType(System.String)
    col.HeaderText = "SomeText"
    col.Name = "colWhateverName"
    GridTemplate.Columns.Add(col)

当我运行它时,网格正确显示,末尾有额外的列。我在这里遇到了一个问题:

 For Each row As DataGridViewRow In GridTemplate.Rows
        row.Cells.Item(11).Value = "Does Not Work"
        row.Cells.Item("colWhateverName").Value = "Also Does Not Work"
    Next

为什么网格显示列,但列的值为空。

1 个答案:

答案 0 :(得分:0)

你很亲密。取出项目(“colWhateverName”)并将其缩短为Cells(“colWhateverName”)。

尝试:

   For Each row As DataGridViewRow In dgvReports.Rows
            row.Cells("colWhateverName").Value = "Also Does Not Work"
        Next