Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding DataGridViewRow manually causes empty cells

This works:

DataGridView.Rows.Add("Bla Bla Bla", value1, value2)

But if I do:

Dim newrow As New DataGridViewRow
newrow.SetValues("Bla Bla Bla", value1, value2)
If Not value1.Equals(value2) Then
  newrow.DefaultCellStyle.ForeColor = Color.Red
End If
DataGridView.Rows.Add(newrow)

the entire row is empty.

Why is the row full of empty cells?

like image 704
Bastiflew Avatar asked Jan 06 '12 16:01

Bastiflew


1 Answers

Your newrow variable does not have any cells, and SetValues is ignoring your information because there aren't any cells to set values to.

From DataGridViewRow.SetValues Method:

If there are more values in the values list than there are cells to be initialized, this method ignores the extra values and returns false. This method also returns false if any of the specified values cannot be set.

If there are fewer values than there are cells, the remaining unmatched cells retain their current values.

Use the CreateCells method to populate the cells:

newrow.CreateCells(DataGridView)
'' or
newrow.CreateCells(DataGridView, New Object() {"Bla Bla Bla", value1, value2})
like image 159
LarsTech Avatar answered Nov 15 '22 06:11

LarsTech