【问题标题】:How to get cell column value from datagridview如何从datagridview获取单元格列值
【发布时间】:2015-01-29 01:02:54
【问题描述】:

如何从datagridview获取单元格列值,如果等于OVERDUE,后面的单元格颜色会改变。

----------------------------------
id     |   uname    |  lname     |
---------------------------------
1      |   anne|    |    a       |
----------------------------------
2      |   rays     |    b       |
----------------------------------
3      |   saws     |  OVERDUE   |     <---------- this row will become yellow This row only.
----------------------------------

我想得到列 lname ,怎么做?谁能给出一些示例编码?

这是我的代码:

  If gridalarmnotice(20, gridalarmnotice.CurrentCell.RowIndex).Value.ToString = "OVERDUE" Then
      gridalarmnotice.DefaultCellStyle.BackColor = Color.Yellow  
  End If

如果列和当前单元格等于 OVERDUE,我想更改背景色。

【问题讨论】:

  • 你想获取姓氏列,但你想确保列和单元格等于过期;这没有意义,请详细说明。
  • 列 lname 的示例,如果有“过期”,则当前单元格的整行将是黄色的,不是所有的表格而是只有当前的单元格。
  • 所以姓氏过期了?你还说当前单元格的整行都是黄色的,你确定。在你提到那个单元之前......
  • 是的!如果当前列单元格包含“OVERDUE”。
  • 好的,既然我明白了你的需要,就给我一点吧。

标签: vb.net


【解决方案1】:

这对你很有用。

把这个方法放在你想要的任何地方......然后调用它。 .例如:按钮点击事件...

 Private Sub HighlightRows()
    'Loop through the rows, if the current rows cell equals "OVERDUE"
    'change the rows color to yellow
    For i As Integer = 0 To gridalarmnotice.Rows.Count - 1
        If gridalarmnotice.Rows(i).Cells("lname").Value.Equals("OVERDUE") Then
            'Then color the whole row, each cell specifically
            For Each cell As DataGridViewCell In gridalarmnotice.Rows(i).Cells
                cell.Style.BackColor = Color.Yellow
            Next
        End If
    Next
 End Sub

附:这是我的测试截图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    • 2021-03-27
    相关资源
    最近更新 更多