简体   繁体   中英

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.

 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").

try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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