0
我在表單的load事件中綁定了一個datagridview。當datagridview綁定時,我修改了一些背景行。Datagridview backcolor在第一次加載時不起作用
在綁定之前,我使用Me.SuspendLayout()
,當它結束時,我使用Me.ResumeLayout
。
當我第一次加載表單時,所有行都使用了defaultCellStyle
。但是,當我通過comboBox上的事件重新加載它時,它按我的預期工作(某些行具有修改後的背景色)。我試圖以編程方式更改comboBox的selectedIndex,但它也不起作用。
我試圖刪除佈局方法,但它不起作用。有人可以給我建議嗎?
順便說一句,我正在開發VB.NET(Visual Studio 2010)。
編輯:
Private Sub frm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.SuspendLayout()
loadDTGV()
Me.ResumeLayout()
myCombo.SelectedIndex = 1
End Sub
Private Sub loadDTGV()
Dim dtBindSource As New BindingSource()
Dim lst As SortedBindingList.SortedBindingList(Of myClass)
lst = _DAO.getData()
dtBindSource.DataSource = lst
dtgv.DataSource = dtBindSource
If dtgv.Rows.Count > 0 Then
colorRows()
End If
End Sub
Private Sub colorRows()
Dim grayStyle As New DataGridViewCellStyle
grayStyle.BackColor = Color.LightGray
For i = 0 To dtg.Rows.Count - 1
If dtg.Rows(i).Cells(0).Value = "TEST" Then
dtg.Rows(i).DefaultCellStyle = grayStyle
End If
Next
End Sub
Public Sub changeIndex() Handles myCMB.SelectedIndexChanged
loadDTGV()
End Sub
可以請你給我[mcve] – Mederic
@Mederic我編輯我的文章 – nbadaud
我的猜測是,在第一次加載你的數據網格沒有行時,所以ColorRows不會被調用。第一次加載時實際上是否包含任何行? – Slugsie