Forum Discussion

ravi_pandey's avatar
ravi_pandey
New Contributor
12 years ago

How to get background color of a Microsoft Data Grid view cell?

Hi,

I'm trying to get background color of a Microsoft Data Grid view cell.




Sub



GetBKColor



 



Dim grid, view, columnId, rowIndex, column, color, attr



Set



grid=



 



Sys.Process("iMakeHarris50056")._



WinFormsObject("MainMenu").WinFormsObject("MdiClient", "")._



WinFormsObject("frmUpdateCalendar0602")._



WinFormsObject("grpProductionCalendar").WinFormsObject("tctrlProdCalendar")._



WinFormsObject("TabPage1").WinFormsObject("SplitContainer1")._



WinFormsObject("SplitterPanel", "", 2).WinFormsObject("dgridProdCalendar")._



WinFormsObject("scOuter").WinFormsObject("SplitterPanel", "")._



WinFormsObject("scGrid").WinFormsObject("SplitterPanel", "")._



WinFormsObject("scFilter").WinFormsObject("SplitterPanel", "")



 



Set view = grid.WinFormsObject("iRelyDataGrid")



columnId = 3



rowIndex = 0



 



 



 



' Get the specified cell and obtain its backcolor



Set column = GetColumn (grid, view, columnId)



 



 



'Set color = view.ViewInfo.GetGridCellInfo_2(rowIndex, column).Appearance.BackColor



Set Color=view.get_GridColor



 



 



'Set color = view.BackColor



 



 



' Post the results to the test log



Set attr =



 



Log.CreateNewAttributes



attr.BackColor = RGB(color.R, color.G, color.B)



Call



 



Log.Message("The backcolor of the specified cell is: " & color.Name, "", pmNormal, attr)



End



Sub



 



Function



GetColumn (Grid, View, ColumnId)



Dim Columns, Col, i



 



' Get the grid view if it is not specified



If View Is Nothing Then



Set View = Grid



End If



 



Set Columns = view.Columns



 



 



' Check whether the column is specified by caption or index



If



 



aqObject.GetVarType (ColumnId) = varOleStr Then



 



 



' Search for the column by its caption



For i = 0 To Columns.Count-1



Set Col = Columns.Item_2(i)



If Col.Caption.OleValue = ColumnId Then



Set GetColumn = Col



 



' The column is found



Exit Function



End If



Next



Set GetColumn = Nothing



 



' The column is not found



Else



 



 



' The column is specified by index



Set GetColumn = Columns.Item_2 (ColumnId)



End If



End



Function



 



Sub



CorrectRGBComponent(ByRef component)



component =



 



aqConvert.VarToInt(component)



If (component < 0) Then



component = 0



Else



If (component > 255) Then



component = 255



End If



End If



End



Sub



 



Function



RGB(r, g, b)



Call CorrectRGBComponent(r)



Call CorrectRGBComponent(g)



Call CorrectRGBComponent(b)



RGB = r + (g * 256) + (b * 65536)



End



Function

But this code gives the background color of complete grid but cell or row or column. Any suggestions?


No RepliesBe the first to reply