Home > Enterprise >  Can a DataGridViewRow be created in a function, independently of a control, and then added to an ari
Can a DataGridViewRow be created in a function, independently of a control, and then added to an ari

Time:04-05

Can a DataGridViewRow be created in a function, independently of a control, and then added to an arbitrary control by the calling procedure? Or must the row be created in association with a control, and then manipulated as needed?

I'm creating a VB.Net form with multiple DataGridView controls. They all have a similar purpose, with identical numbers & names of columns. I would like to create the rows & cells independently in a function, and then add them to the correct control after the function returns.

Here's a simplified example of what I'm trying to do. This does not work. I receive the error 'New' cannot be used on a class that is declared 'MustInherit', but I cannot find the solution.

Private Sub frmDataGridViewCellTest_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DataGridView1.Columns.Add("colA", "A")
    DataGridView1.Columns.Add("colB", "B")
    DataGridView1.Columns.Add("colC", "C")
    Dim r As DataGridViewRow = CreateRow()
    DataGridView1.Rows.Add(r)
End Sub

Private Function CreateRow() As DataGridViewRow
    Dim r As New DataGridViewRow 'Create a new row independently of any DataGridView control.
    Dim i As Integer
    i = r.Cells.Add(New DataGridViewCell) 'error here
    r.Cells.Item(i).Value = "Hello"
    i = r.Cells.Add(New DataGridViewCell) '...and here
    r.Cells.Item(i).Value = "world"
    i = r.Cells.Add(New DataGridViewCell) '...and here
    r.Cells.Item(i).Value = "!"
    Return r
End Function

CodePudding user response:

A DataGridView doesn't contain vanilla DataGridViewCell objects. Each cell is a specific type that inherits DataGridViewCell. When you add a row via the grid itself, the cells are generated based on the columns, e.g. the cell generated for a DataGridViewTextBoxColumn will be a DataGridViewTextBoxCell. That's the default column type so that's what your columns will be, so that's what you need to make your cells.

CodePudding user response:

When you create a new DataGridViewRow, you need also to initialize its DataGridViewCellCollection before you can add their values. The cells are created and initialized according to what type is defined in the DataGridViewColumn.CellTemplate property of each DataGridViewColumn. For this, create a new row then call the CreateCells method which needs a DataGridView parameter to get the template of each cell as mentioned.

Wherefore, you can refactor the CreateRow function as follows:

Private Function CreateRow(dgv As DataGridView) As DataGridViewRow
    Dim row = New DataGridViewRow
    row.CreateCells(dgv, "Hello", "World", "!")
    Return row
End Function

' And maybe
Private Function CreateRow(dgv As DataGridView,
                            value1 As String,
                            value2 As String,
                            value3 As String) As DataGridViewRow
    Dim row = New DataGridViewRow
    row.CreateCells(dgv, value1, value2, value3)
    Return row
End Function

The caller:

Dim newRow = CreateRow(DataGridView1)
DataGridView1.Rows.Add(newRow)

'Or
Dim newRow = CreateRow(DataGridView1, "Hello", "Word", "!")
DataGridView1.Rows.Add(newRow)
  • Related