2017-05-18 42 views
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 
+1

可以请你给我[mcve] – Mederic

+0

@Mederic我编辑我的文章 – nbadaud

+0

我的猜测是,在第一次加载你的数据网格没有行时,所以ColorRows不会被调用。第一次加载时实际上是否包含任何行? – Slugsie

回答

0

我发现了大约同样的问题,其中一些文章,似乎有与datagridview控制的问题(我们不能改变颜色的细胞,直到形式已被证明)。方法运行和事件被触发,但不会改变颜色。

要解决我的问题,我将我的方法放在DataBindingComplete事件中的颜色行中。现在它工作得很好。

相关问题