Adding a DataGridViewRow manually causes empty cells

It works:

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

But if I do this:

 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 whole line is empty.

Why is a row filled with empty cells?

+4
source share
1 answer

There are no cells in your newrow variable, and SetValues ignores your information because there are no cells to set the values.

From the DataGridViewRow.SetValues ​​Method:

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

If there are fewer values ​​than cells, the remaining inconsistent 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}) 
+4
source

Source: https://habr.com/ru/post/1389685/


All Articles