Forum Discussion

sarya's avatar
sarya
Frequent Contributor
14 years ago

How to obtain a cell's background color in datagridview table

Hi,



I need to obtain the background colour of a cell (blue in my case) when the cell is clicked. I was able to obtain the value from the object's property but not able to obtain the colour.



The table's code is like this:


Sys.Process("Burster").WinFormsObject("frmViewLookupValues").WinFormsObject("SplitContainer1").WinFormsObject("SplitterPanel", "", 1).WinFormsObject("dgvData")

.



Then I used this using Log.Message but it returns nothing:

Sys.Process("Burster").WinFormsObject("frmViewLookupValues").WinFormsObject("SplitContainer1").WinFormsObject("SplitterPanel", "", 1).WinFormsObject("dgvData").Rows.Item(1).DataGridView.Item(1, 0).DataGridView.BackColor ;



Thanks,

Sumedha




.Process("Burster").WinFormsObject("frmViewLookupValues").WinFormsObject("SplitContainer1").WinFormsObject("SplitterPanel", "", 1).WinFormsObject("dgvData") .Then I used this using Log.Message but it returns nothing:Sys.Process("Burster").WinFormsObject("frmViewLookupValues").WinFormsObject("SplitContainer1").WinFormsObject("SplitterPanel", "", 1).WinFormsObject("dgvData").Rows.Item(1).DataGridView.Item(1, 0).DataGridView.BackColor ;Thanks,Sumedha

8 Replies


  • Hi Sumedha,





    Since your application is a .NET application, TestComplete works with it as with an Open application and can access the native methods and properties of the application objects. So, you can use these native methods and properties of the needed control to get the background color. If you do not know which methods or properties should be used, you can ask the application developers to help you find them.





    There is another way to get a background color: analyze the image of the control. You can find a sample script demonstrating this approach in the Get a control's background color article.
  • sarya's avatar
    sarya
    Frequent Contributor
    Hi David,



    I talked to developer and he said that the code used backcolor.

    I used this code:




    function Test ()


    Test ()

    {


    var p, Grid, RowIndex, ColIndex, CellValue;


    // Obtain the grid object


    burster = Sys.Process("Burster");


    dataGridView = burster.frmViewLookupValues.WinFormsObject("SplitContainer1").WinFormsObject("SplitterPanel", "", 1).dgvData;

    // Get indexes of the focused row and column

    RowIndex = dataGridView.CurrentCellAddress.Y;

    ColIndex = dataGridView.CurrentCellAddress.X;

    Log.Message ("Focused row: " + RowIndex);

    Log.Message ("Focused column: " + dataGridView.wColumn(ColIndex));

    CellValue = dataGridView.CurrentCell.Style.BackColor();

    if (aqObject.GetVarType (CellValue) == varDispatch)

    Log.Message ("Focused cell value: " + CellValue.ToString().OleValue)

    else

    Log.Message ("Focused cell value: " + CellValue);


    }



    I used this in the code seeing from object's properties. " CurrentCell.Style.get_BackColor() "



    The results are like this:  Focused row: 1 ,Focused column: Key Value , Focused cell value: Color [Highlight] .



    The results show the cellValue as Color [Highlight] but does not show result as "blue" .



    I also attached the properties if you can get something from that.



    Thanks,

    Sumedha


  • Hi Sumedha,





    Actually, Highlight is a standard color macro. Even if it is blue on your system, it can be gray or green or another color on a different machine as this depends on the machine settings. So, I think that you can use this value as a name of a color without problems.
  • sarya's avatar
    sarya
    Frequent Contributor
    Thank for the reply David. So can I not verify in someway if the color is blue as the color is steady for me on any machine ?



    Thanks,

    sumedha

  • Hi Sumedha,





    If you want to get the numerical value of the color, you can try calling the ToArgb() method of the color object. If you need to get the Blue string, I think that you will have to create a table containing integer values of colors and the corresponding color names.





    If you want to get the color by its parts, you can do this using this code:

    var clr = ...

    Log.Message(clr.FromArgb(clr.ToArgb()).ToString());
  • sarya's avatar
    sarya
    Frequent Contributor
    Hi David,



    Thanks for the solution. I was able to obtain the color values  as cell value: Color [A=255, R=10, G=36, B=106] .So if I use 'Attr' property as Attr.Fontcolor  in log.message,it highlights the message in blue but can I not obtain the string value as in 'Blue' using these code as this code outputs the value as in number and not in the color's name.



    function CorrectRGBComponent(component)

    {

      component = aqConvert.VarToInt(component);

      if (component < 0)

        component = 0;

      else

        if (component > 255)

          component = 255;

      return component;

    }



    function RGB(r, g, b)

    {

      r = CorrectRGBComponent(r);

      g = CorrectRGBComponent(g);

      b = CorrectRGBComponent(b);

      return r | (g << 8) | (b << 16);

    }





    Thanks,

    sumedha

  • Hi Sumedha,





    There is no a built-in way to get the 'Blue' string in your case. Such description of the color you see does not exist in the system at all. The color used in your grid is 'Highlight' and it is defined as '[A=255, R=10, G=36, B=106]'. If you want to get the 'Blue' string, you need to create a special script routine that will check the color components (R, G and B) and return the string corresponding to them.
  • sarya's avatar
    sarya
    Frequent Contributor

    Thanks David. I am checking against the color components (R, G and B) and returning the string corresponding to them.



    Thanks,

    Sumedha