How to change a row color of datagridview without looping in datatable
Datagridview1 was bounding with Datatable. This DataTable is stored color value for painting each row. And a BindingSource was used for filtering.
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
I need to get color value from each datarow in Datatable then use it for paint each row in datagridview.
And if I have a lot of datarow (1000-10,0000 rows) and I need to filtered Datagridview like this
bs.Filter = "Checked = True"
That it's mean I must iterate in all datarows for get each color value and set back to 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
It isn't bad for large datatable.
How to Datagridviewrow by using a value from datarow?