【发布时间】:2017-05-18 13:35:52
【问题描述】:
我的表单加载事件中有一个绑定数据网格视图的方法。绑定 datagridview 时,我修改了一些 backcolor 行。
绑定前我使用Me.SuspendLayout(),绑定完成后我使用Me.ResumeLayout。
当我的表单第一次加载时,所有行都使用defaultCellStyle。但是,当我通过组合框上的事件重新加载它时,它会按我的预期工作(某些行具有修改后的背景色)。我试图以编程方式更改组合框的 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
【问题讨论】:
-
@Mederic 我编辑了我的帖子
-
我的猜测是,在第一次加载时,您的数据网格没有行,因此不会调用 ColorRows。首次加载时 lst 实际上是否包含任何行?
-
@Slugsie 在第一次加载中,我的 datagridview 包含 1105 行。
-
它是否真的命中了'dtg.Rows(i).DefaultCellStyle = grayStyle'这一行?
标签: vb.net winforms datagridview