简体   繁体   English

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

[英]Unable to set value of databound DataGridView added column

I have a DataGridView that is bound in code behind, I add a column, then i need to insert data into that column based off column in the same row. 我有一个在后面的代码中绑定的DataGridView,我添加了一列,然后我需要根据同一行中的列将数据插入到该列中。

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

I add a column to the grid 我在网格中添加一列

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

When i run it, the grid displays correctly with the extra column on the end. 当我运行它时,网格将正确显示,并在末尾显示额外的列。 I run into an issue here: 我在这里遇到一个问题:

 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

Why is the grid showing the column, but the value of the column is empty. 为什么网格显示该列,但该列的值为空。

You are close. 你近了 Take out the item("colWhateverName") and shorten it to Cells("colWhateverName"). 取出项目(“ colWhateverName”)并将其缩短为Cells(“ colWhateverName”)。

try: 尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM