DataGridViewRow 是否可以独立于控件在函数中创建,然后由调用过程添加到任意控件中?
Can a DataGridViewRow be created in a function, independently of a control, and then added to an aribtrary control by the calling procedure?
能否在函数中独立于控件创建 DataGridViewRow,然后通过调用过程将其添加到任意控件?或者必须与控件关联创建该行,然后根据需要进行操作?
我正在创建一个包含多个 DataGridView 控件的 VB.Net 表单。它们都有相似的目的,具有相同的列数和名称。我想在函数中独立创建行和单元格,然后将它们添加到函数后的正确控件中 returns.
这是我正在尝试做的一个简化示例。这是行不通的。我收到错误 'New' cannot be used on a class that is declared 'MustInherit'
,但找不到解决方案。
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
A DataGridView
不包含原版 DataGridViewCell
对象。每个细胞都是继承 DataGridViewCell
的特定类型。当您通过网格本身添加一行时,单元格是根据列生成的,例如为 DataGridViewTextBoxColumn
生成的单元格将是 DataGridViewTextBoxCell
。这是默认的列类型,所以这就是您的列的类型,所以这就是您制作单元格所需的类型。
当您创建一个新的 DataGridViewRow
时,您还需要初始化它的 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
方法,该方法需要一个 DataGridView
参数来获取每个单元格的模板。
因此,您可以重构 CreateRow
函数,如下所示:
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
来电者:
Dim newRow = CreateRow(DataGridView1)
DataGridView1.Rows.Add(newRow)
'Or
Dim newRow = CreateRow(DataGridView1, "Hello", "Word", "!")
DataGridView1.Rows.Add(newRow)
能否在函数中独立于控件创建 DataGridViewRow,然后通过调用过程将其添加到任意控件?或者必须与控件关联创建该行,然后根据需要进行操作?
我正在创建一个包含多个 DataGridView 控件的 VB.Net 表单。它们都有相似的目的,具有相同的列数和名称。我想在函数中独立创建行和单元格,然后将它们添加到函数后的正确控件中 returns.
这是我正在尝试做的一个简化示例。这是行不通的。我收到错误 'New' cannot be used on a class that is declared 'MustInherit'
,但找不到解决方案。
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
A DataGridView
不包含原版 DataGridViewCell
对象。每个细胞都是继承 DataGridViewCell
的特定类型。当您通过网格本身添加一行时,单元格是根据列生成的,例如为 DataGridViewTextBoxColumn
生成的单元格将是 DataGridViewTextBoxCell
。这是默认的列类型,所以这就是您的列的类型,所以这就是您制作单元格所需的类型。
当您创建一个新的 DataGridViewRow
时,您还需要初始化它的 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
方法,该方法需要一个 DataGridView
参数来获取每个单元格的模板。
因此,您可以重构 CreateRow
函数,如下所示:
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
来电者:
Dim newRow = CreateRow(DataGridView1)
DataGridView1.Rows.Add(newRow)
'Or
Dim newRow = CreateRow(DataGridView1, "Hello", "Word", "!")
DataGridView1.Rows.Add(newRow)