Datagridview1与Datatable绑定。此DataTable存储用于绘制每一行的颜色值。然后使用BindingSource进行过滤。
Dim dt As New DataTable
Dim dcColor As New DataColumn("Color", GetType(Color))
Dim dcChecked As New DataColumn("Checked", GetType(Boolean))
Dim bs As New BindingSource
dt.Columns.Add(dcColor)
dt.Columns.Add(dcChecked )
bs.DataSource = dt
DataGridView1.DataSource = bs
我需要从Datatable中的每个数据行获取颜色值,然后将其用于绘制datagridview中的每一行。
如果我有很多数据行(1000-10,0000行),我需要像这样过滤Datagridview
bs.Filter = "Checked = True"
这意味着我必须在所有数据行中进行迭代以获取每种颜色值并重新设置为datagridviewrow。
Private Sub DataGridView1_Paint(sender As Object, e As PaintEventArgs) Handles DataGridView1.Paint
Try
For Each dr As DataRow In dt.Rows
Dim needColor As Color = dr.Item("Color")
DataGridView1.Rows(???).DefaultCellStyle.BackColor = needColor
Next
Catch ex As Exception
m_logger.AppendLog(ex)
End Try
End Sub
对于大型数据表来说还不错。
如何使用datarow中的值来Datagridviewrow?