Forum Discussion

ravi_pandey's avatar
ravi_pandey
New Contributor
11 years ago

Cannot get background color of a DevExpress XtraGrid cell

Hi,

I'm trying to get the background color of a DevExpress XtraGrid cell. I went through support article but it won't solve my problem (http://support.smartbear.com/viewarticle/26134/).

I'm using following code:


Sub



GetBKColor



 



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



 



 



 



' Obtain the grid



' Set grid = Sys.Process("GridMainDemo").WinFormsObject("RibbonMainForm")._



' WinFormsObject("panelControl1").WinFormsObject("gcContainer")._



' WinFormsObject("FixedBands").WinFormsObject("xtraTabControl1")._



' WinFormsObject("xtraTabPage1").WinFormsObject("gridControl1")



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 = 5



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



 



 



' 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 after running this routine I'm getting error

Unable to find the object ViewInfo.



So I believe problematic line is:



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



How to solve it?


  • jkrolczy's avatar
    jkrolczy
    Regular Contributor
    I finally got back to this and got it working thanks to the threads provided above for more information and reference to on this change by DevXpress. 



    Thank you very much to all !!!





    Here is my working code example:



    //

    // Get Grid Cell BackColor

    //   Passed in Parm: gridObj   - grid object path

    //   Passed in Parm: columnId  - String or Int

    //   Passed in Parm: rowIndex  - Row Number (int)

    function getGridCellBKColor(gridObj, columnId, rowIndex)



      var view, columnNum, columnObj, color, Attr;

       

      // Get Grid MainView path

      view = gridObj.MainView;

     

      // Get the specified cell and obtain its backcolor

      columnNum = getColumn(gridObj, view, columnId);

      columnObj = gridObj.MainView.Columns.Item_2(columnNum);


      color = gridObj.MainView.ViewInfo.GetGridCellInfo_2(rowIndex, columnObj).Appearance.BackColor;

     

      // Post the results to the test log

      Attr = Log.CreateNewAttributes(); 

      Attr.BackColor = generateRGB(color.R, color.G, color.B);

      Log.Message("The backcolor of the specified cell is: " + color.Name, "", pmNormal, Attr);

     

      return color.Name;

    }



    JamesK


  • Hi Tanya,



    View object does not have ViewInfo property. I checked it through Object browser also. No such property exists for this object.

    If I am working with View.backcolor then routine is returning prominent non white color of the grid but I want color of the particular cell or row or column but full grid.



    Thanks,

    Ravi
  • Hi Tanya,



    I was mistaken that grid under test is DevXpress Xtra grid but I contacted my dev team and they told me that this particular grid is Microsoft Data Grid View. Still problem is not solved. I surfed smartbear support articles but there is no article for how to get background color of Microsoft data grid view cell.
  • jkrolczy's avatar
    jkrolczy
    Regular Contributor
    I want to bring this issue back to life:



    I do have an app using DevExpress XtraGrid.



    My framework previous could use the sample backcolor code and determine

    the backcolor of a specific grid cell.

    This was with DevExpress 9.3



    The app has upgraded to use 11.2

    and the backcolor code no longer works.



    I see MainView

    but after that in the Object spy, ViewInfo is no longer shown.



    I cannot figure out what's changed here that would prevent ViewInfo not

    to be displayed in the Object Spy information.



    This is not a framework I can zip and send for review.

    If code segments are needed, please let me know.



    Regards,

    JamesK
  • jkrolczy's avatar
    jkrolczy
    Regular Contributor
    I hate it when I don't have something turned on in Options to see more items in Object Spy.



    I turned on 'Show hidden members'

    I can see 'ViewInfo' now after selecting MainView in Object Spy.



    I am back to my main issue though  :(



    Scenario desc:

    I have a DevX XtraGrid.

    A grid table of data.

    There is a Search box.

    Upon doing a search, any text found, the cell is highlighted in

    a different color. 

    My script wound find that cell and get the backcolor and verify it

    is the correct color.



    This is what I am trying to get to work again.





    Here is my code sample:



    function getBackcolor()


    {



    gridObj = Sys.Process("XAppsApplication").WinFormsObject("ClaimInbox").WinFormsObject("pnlPrimary").WinFormsObject("pnlMain").

        WinFormsObject("XClaim").WinFormsObject("XClaim.tbMainPanel").WinFormsObject("XClaim.tpMainPanelWorkItems").

     WinFormsObject("XClaim.GridPanel").WinFormsObject("XClaim.grdClaims");



    var columnId = "Patient First Name";



    rowIndex = 5;



    view = gridObj.MainView;



    // Get the specified cell and obtain its backcolor

    var column = getColumn(gridObj, view, columnId);



    Log.Message("column: "+column); // 22



    var color = gridObj.MainView.ViewInfo.GetGridCellInfo_2(rowIndex, column); //.Appearance.BackColor;;



     



    //

    // Attr = Log.CreateNewAttributes();

    // Attr.BackColor = generateRGB(color.R, color.G, color.B);

    // Log.Message("The backcolor of the specified cell is: " + color.Name, "", pmNormal, Attr);

    //



    }





    The '...GetGridCellInfo_2(rowIndex, column);' keeps failing on me.



    I have passed in the parms as (int, int) and as (int, str) and I still get an error with mscorlib:



    (int, int):

    System.ArgumentException: Object of type 'System.Int32' cannot be converted to type 'DevExpress.XtraGrid.Columns.GridColumn'.



    or



    (int, str);

    System.ArgumentException: Object of type 'System.String' cannot be converted to type 'DevExpress.XtraGrid.Columns.GridColumn'.





    I will keep working at this.

    If anyone has any suggestions, please let me know.



    Thanks,

    JamesK









     


  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)
    Hi James,

     


    Judging by this discussion on the Developer Express forum, the GetGridCellInfo method's parameters were changed:


    Starting with version 2011 vol 1.5 the GridViewInfo.GetGridCellInfo method accepts the GridColumn as the second parameter, rather than the column absolute index.


     


    Thus, you need to make sure that the getColumn function always returns the GridColumn object.


     

  • jkrolczy's avatar
    jkrolczy
    Regular Contributor
    Tanya,



    Thanks for the reply and the research you did.



    I will go back and work on on the code to get the GridColumn object

    passed in for where I am having my issue.



    I'll reply back on my progress.



    Thanks,

    JamesK