SCENARIO
I'm manually filling a DataGridView with a DataGridViewRow collection:
I created the columns at design time in the GUI builder of Visual Studio.
DataGridViewTextBoxColumn
. DataGridViewImageColumn
. DataGridViewTextBoxColumn
. DataGridViewComboBoxColumn
. So, when I want to add a new row, I do this:
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
My intention is to follow good programming practices, then, to avoid this kind of interaction with the UI , I just would preffer to use and manage a DataSource
, then how I can create a DataTable
that takes the same type of values to set it as the DataSource
of the control?. Is it possible?.
If not, just what can I do to manage a DataSource
instead of directlly manage the rows collection of the control?
In general, how I can improve what I'm doing for gain better efficiency?.
what can I do to manage a DataSource instead of directlly manage the rows collection of the control
A Class and a collection are pretty easy to implement as a DataSource
and will also it will be pretty easy to modify your MoveUp/Dn methods for it.
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)
After you fill the collection with the items, set it as the DGV's DataSource
:
...
dgvList.Add(New DGVItem(ndx, filename, Compression.Default))
...
myDGV.DataSource = dgvList
You also need to tell the DGV which property to display in which column. The DGV will AutoGenerateColumns
, but you probably already created some using the Designer (IDE). Open the columns editor and for each column, find DataPropertyName
and type in the Item property name to display in that column. For instance, Col 0 would be Index
or Order
. If you do not add a column for the new Selected
property, it wont show.
In cases where you let the DGV auto-create columns from the DataSource
, after you bind the source to the control, you can remove any unwanted columns (such as Selected
). Note that it will create columns but it will not name them.
At this point, you would be utilizing the "View" aspect of the DGV - it is displaying the data contained elsewhere. As such, you no longer manipulate the DGV rows (such as MoveRow Up/Dn or deleting a row) - that will result in an error. Instead you manage the BindingList
- changes to it will automatically display in the DGV.
Finally, note that the DGV will update the BindingList
contents when the user performs edits. If they pick a different compression for item 3, dgvList(2).Compression
will be updated for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.