更改动态创建的datagridview的单元格颜色

时间:2013-11-20 14:56:29

标签: vb.net winforms

我有一组动态数据网格,显示从数据库填充的队列(生产项目状态)

'The datagridviews have already been created at form load.

'get the Qs list - returns datatable with list of queuenames.
    Qlist = dbconnect.dbCall("SELECT * FROM QueueList")

   For iQlist = 0 To Qlist.Rows.Count - 1
'iterate the queue List and populate the datagridview for showing that queue with any items in that queue

     listContents = dbconnect.dbCall("SELECT itemName,queueEntryTime FROM " & Qlist.Rows(iQlist).Item("Q_name"))

      'get the dynamic name of the datagridview 
      Dim controlName = "dg_" & Qlist.Rows(iQlist).Item("Q_name")

      'assign the datatable containing its items to that datagridview
      Me.tabQsAndServiceBays.Controls.Item(controlName).dataSource = listContents

      'want to colour items in each datagrid that are over certain age.
      'i.e. the items is in production queue for too long, something has gone wrong.

      For Each dr As DataGridViewRow In tabQsAndServiceBays.Controls.Item(controlName).rows
            'condition for each row based on cell's contents, if > 20mins mark different color.
             If tabQsAndServiceBays.Controls.Item(controlName).Cells("queueEntryTime").Value > Now - 20 Then
                 dr.DefaultCellStyle.BackColor = Color.LightGreen
             End If
    'next


Next

我收到Public member 'rows' on type 'DataGrid' not found错误。

为什么我可以引用数据网格视图的.datasource属性,但不能引用.rows

1 个答案:

答案 0 :(得分:0)

如果这是实际错误消息,'DataGrid'而不是'DataGridView',那么看起来NET认为您正在使用与DGV不可互换的DataGrid。这里:

' 'item' is not needed, removed to prevent scroll
For Each dr As DataGridViewRow In tabQsAndServiceBays.Controls(controlName).rows

这里有2个可能的错误。首先,DataGridViewRow只能与DGV一起使用,因此无论如何,如果使用的是DataGrid,都会失败。其次,DataGrids没有公共Rows属性。 DataGrid控件用于向后兼容和IIRC,您基本上通过底层ADO数据集访问行。