改进 DataGridView 行管理
Improve DataGridView rows managing
SCENARIO
我正在用 DataGridViewRow 集合手动填充 DataGridView:
我在设计时在 Visual Studio 的 GUI 构建器中创建了列。
- 第一列采用 整数 值,它是
DataGridViewTextBoxColumn
.
- 第二列有一个 Icon 对象,它是一个
DataGridViewImageColumn
.
- 第三列采用 String 值,它是
DataGridViewTextBoxColumn
.
- 第四个采用 String 值,它是
DataGridViewComboBoxColumn
.
所以,当我想添加新行时,我会这样做:
Dim dgvr As New DataGridViewRow
With dgvr
.CreateCells(MyDataGridView)
.Cells(0).Value = An Integer value
.Cells(1).Value = An Icon object
.Cells(2).Value = An String value
.Cells(3).Value = An existing ComBoBox item name.
End With
MyDataGridView.Rows.Add(dgvr)
QUESTION
我的意图是遵循良好的编程习惯,然后,为了避免与 UI 的这种交互,我更愿意使用和管理一个 DataSource
,那么我如何创建一个 DataTable
并采用相同类型的值来将其设置为控件的 DataSource
?。有可能吗?
如果没有,我可以做些什么来管理 DataSource
而不是直接管理控件的行集合?
一般来说,我如何改进我正在做的事情以获得更高的效率?
what can I do to manage a DataSource instead of directlly manage the rows collection of the control
A Class 和一个集合很容易作为 DataSource
实现,并且也很容易为它修改 MoveUp/Dn 方法。
Class DGVItem
Public Property Index As Integer
Public Property Name As String
Public Property Color As String
' this will make the up/dn method simpler
Public Property Selected As Boolean
Public Sub New(i As Integer, n As String, v As String)
Index = i
Name = n
Color = v
End Sub
Public Overrides Function ToString() As String
Return String.Format("{0} ({1})", Name, Index)
End Function
End Class
' collection source:
Private dgvList As BindingList(Of DGVItem)
用项目填充集合后,将其设置为 DGV 的 DataSource
:
...
dgvList.Add(New DGVItem(ndx, filename, Compression.Default))
...
myDGV.DataSource = dgvList
您还需要告诉 DGV 属性 在哪一列显示。 DGV 将 AutoGenerateColumns
,但您可能已经使用设计器创建了一些 (IDE)。打开列编辑器,对于每一列,找到 DataPropertyName
并输入要显示在该列中的项目 属性 名称。例如,第 0 列将是 Index
或 Order
。如果您不为新 Selected
属性 添加一列,它将不会显示。
在您让 DGV 从 DataSource
自动创建列的情况下,将源绑定到控件后,您可以删除任何不需要的列(例如 Selected
)。请注意,它会创建列,但不会为其命名。
此时,您将利用 DGV 的 "View" 方面 - 它正在显示别处包含的数据。因此,您不再操作 DGV 行(例如 MoveRow Up/Dn 或删除行)——这将导致错误。相反,您管理 BindingList
- 对其所做的更改将自动显示在 DGV 中。
最后,请注意 DGV 将在用户执行编辑时更新 BindingList
内容。如果他们为项目 3 选择了不同的压缩,dgvList(2).Compression
将为您更新。
SCENARIO
我正在用 DataGridViewRow 集合手动填充 DataGridView:
我在设计时在 Visual Studio 的 GUI 构建器中创建了列。
- 第一列采用 整数 值,它是
DataGridViewTextBoxColumn
. - 第二列有一个 Icon 对象,它是一个
DataGridViewImageColumn
. - 第三列采用 String 值,它是
DataGridViewTextBoxColumn
. - 第四个采用 String 值,它是
DataGridViewComboBoxColumn
.
所以,当我想添加新行时,我会这样做:
Dim dgvr As New DataGridViewRow
With dgvr
.CreateCells(MyDataGridView)
.Cells(0).Value = An Integer value
.Cells(1).Value = An Icon object
.Cells(2).Value = An String value
.Cells(3).Value = An existing ComBoBox item name.
End With
MyDataGridView.Rows.Add(dgvr)
QUESTION
我的意图是遵循良好的编程习惯,然后,为了避免与 UI 的这种交互,我更愿意使用和管理一个 DataSource
,那么我如何创建一个 DataTable
并采用相同类型的值来将其设置为控件的 DataSource
?。有可能吗?
如果没有,我可以做些什么来管理 DataSource
而不是直接管理控件的行集合?
一般来说,我如何改进我正在做的事情以获得更高的效率?
what can I do to manage a DataSource instead of directlly manage the rows collection of the control
A Class 和一个集合很容易作为 DataSource
实现,并且也很容易为它修改 MoveUp/Dn 方法。
Class DGVItem
Public Property Index As Integer
Public Property Name As String
Public Property Color As String
' this will make the up/dn method simpler
Public Property Selected As Boolean
Public Sub New(i As Integer, n As String, v As String)
Index = i
Name = n
Color = v
End Sub
Public Overrides Function ToString() As String
Return String.Format("{0} ({1})", Name, Index)
End Function
End Class
' collection source:
Private dgvList As BindingList(Of DGVItem)
用项目填充集合后,将其设置为 DGV 的 DataSource
:
...
dgvList.Add(New DGVItem(ndx, filename, Compression.Default))
...
myDGV.DataSource = dgvList
您还需要告诉 DGV 属性 在哪一列显示。 DGV 将 AutoGenerateColumns
,但您可能已经使用设计器创建了一些 (IDE)。打开列编辑器,对于每一列,找到 DataPropertyName
并输入要显示在该列中的项目 属性 名称。例如,第 0 列将是 Index
或 Order
。如果您不为新 Selected
属性 添加一列,它将不会显示。
在您让 DGV 从 DataSource
自动创建列的情况下,将源绑定到控件后,您可以删除任何不需要的列(例如 Selected
)。请注意,它会创建列,但不会为其命名。
此时,您将利用 DGV 的 "View" 方面 - 它正在显示别处包含的数据。因此,您不再操作 DGV 行(例如 MoveRow Up/Dn 或删除行)——这将导致错误。相反,您管理 BindingList
- 对其所做的更改将自动显示在 DGV 中。
最后,请注意 DGV 将在用户执行编辑时更新 BindingList
内容。如果他们为项目 3 选择了不同的压缩,dgvList(2).Compression
将为您更新。