2014-04-28 98 views
0

我有一个DataGridView(WinForms,C#)。我想要做的就是将网格划分为左/右部分。所以,当我点击左侧部分(3,3)时会突出显示,例如点击右侧部分(6,6)时会突出显示。Datagridview自定义行高亮

Col1 | Col2 | Col3 | Col4 
------------------------- 
1  1  2  2 
3  3  4  4 
5  5  6  6 

获得价值不成问题,但亮点..?

int iCol = dgv.CurrentCell.ColumnIndex; 
    if (iCol == 0 || iCol == 1) // left side 
    { 

    } 
    else // right side 
    { 
    } 

回答

0

我希望这可以帮助你,但你不应该直接在代码编写列索引,我觉得coltroling指标与数学表达式是更好的解决方案。 此代码对于大数据也会很慢,所以您可以使用列选择。但是,如果您使用列选择,则会在您单击列标题时阻止对行进行重新排序。

Private Sub DataGridView1_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick 
    DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect 
    If e.ColumnIndex < DataGridView1.ColumnCount/2 Then 
     For Each c As DataGridViewColumn In DataGridView1.Columns 
      If c.Index < DataGridView1.ColumnCount/2 Then 
       For Each r As DataGridViewRow In DataGridView1.Rows 
        r.Cells(c.Index).Style.BackColor = Color.Blue 
       Next 
      Else 
       For Each r As DataGridViewRow In DataGridView1.Rows 
        r.Cells(c.Index).Style.BackColor = Color.Gray 
       Next 
      End If 
     Next 
    Else 
     For Each c As DataGridViewColumn In DataGridView1.Columns 
      If c.Index >= DataGridView1.ColumnCount/2 Then 
       For Each r As DataGridViewRow In DataGridView1.Rows 
        r.Cells(c.Index).Style.BackColor = Color.Blue 
       Next 
      Else 
       For Each r As DataGridViewRow In DataGridView1.Rows 
        r.Cells(c.Index).Style.BackColor = Color.Gray 
       Next 
      End If 
     Next 
    End If 
End Sub 
+0

对不起,这里错误的解释。但我想突出显示行(3,3),而不是第2列(左侧部分) – user3200249

+0

好吧,它没有太多不同的代码。使用dataGirdView.Rows(e.RowIndex).selected = True,只有一行使用相同的逻辑,但这是逻辑真实代码实现会有些不同 – Red