Forum Discussion

Darshana's avatar
Darshana
Occasional Contributor
9 years ago
Solved

How to get Native property(columnByname) of TDBGrid in Testcomplete

Hi ,

 

I am using TDB Grid and I want to use Native property for the object TDBGrid.

 

I want to use the following line of code which i can found from another online script.

 

  WinFormsObject("gridControl").MainView.Columns.ColumnByName("gridColumnName").visible

 

But I could not find the "ColumnByName("gridColumnName").visible" option on my property list.

 

Can you please guide me how can I make enable theis property so i can use it?

 

 

Thanks

Darshana

  • TDBGrid.

     

    This is the Delphi grid you're referring to right?

     

    First up, you will need debug info enabled in the compiled Delphi module. Without that, you won't get anywhere.

     

    See: https://support.smartbear.com/viewarticle/68476/

     

    For the grids themselves ...

     

    The column names are not that simple. It depends how your developers have implemented it.

     

    The grid you see in a Delphi TDBGrid is only an overlay. There is also an underlying recordset object which contains all the actual data. The columns do have a displayname property. But it is not always set. And it does not always match the column name in the underlying record set. As I say, it depends how your devs have done it.

     

    (I'm using C# script here .... which is essentially the same as Jscript I believe. VBScript is my usual choice)

     

    I check the display name first:

     

     

    var Obj_Grid = <my grid>;
    var disp_name = Obj_Grid["Columns"](<column_number>)["DisplayLabel"];

     

    This may come back empty. If it hasn't been populated by the devs, it will automatically be inherited from the underlying dataset. In which case, you have to use the field name of the column (which may, or may not, be the same as the column name you see on screen):

     

    var match_field = Obj_Grid["Columns"](<column_number>)["FieldName"];

     

    Then get the field count for the recordset (there can be hidden ones - you can't assume the number of fields in the recordset is the same as the number of visible columns you can see on screen):

     

    var field_count = Obj_Grid["DataLink"]["DataSet"]["FieldList"]["Count"];

     

    Then iterate though the fields (fields are effectively columns in the underlying dataset) looking for a match with the field name found in the column in the GUI. The field name in the dataset can be found as follows:

     

    var field_name = Obj_Grid["DataLink"]["DataSource"]["DataSet"]["Fields"]["Fields"](<dataset field/column number>)["FullName"];

    When you hit a match for that name, vs the field name you got from the GUI column, you have got the right column/field in the dataset. (aka - you have found the field/column in the dataset which is providing the data for the column you actually see on screen)

     

    Columns in the GUI and fields in the dataset are VERY closely linked. The two combined effectively form what you see on the screen. But you have to almost treat them as two separate objects. They each have their own distinct set of controls and methods. They are all sorts of fun to work with!

     

    Fortunately for me, I have a couple of experienced Delphi developers on hand to help me out with the controls.

     

    Of course, if it's not a Delphi DB Grid you're dealing with, disregard all the above!

     

    :)

1 Reply

  • TDBGrid.

     

    This is the Delphi grid you're referring to right?

     

    First up, you will need debug info enabled in the compiled Delphi module. Without that, you won't get anywhere.

     

    See: https://support.smartbear.com/viewarticle/68476/

     

    For the grids themselves ...

     

    The column names are not that simple. It depends how your developers have implemented it.

     

    The grid you see in a Delphi TDBGrid is only an overlay. There is also an underlying recordset object which contains all the actual data. The columns do have a displayname property. But it is not always set. And it does not always match the column name in the underlying record set. As I say, it depends how your devs have done it.

     

    (I'm using C# script here .... which is essentially the same as Jscript I believe. VBScript is my usual choice)

     

    I check the display name first:

     

     

    var Obj_Grid = <my grid>;
    var disp_name = Obj_Grid["Columns"](<column_number>)["DisplayLabel"];

     

    This may come back empty. If it hasn't been populated by the devs, it will automatically be inherited from the underlying dataset. In which case, you have to use the field name of the column (which may, or may not, be the same as the column name you see on screen):

     

    var match_field = Obj_Grid["Columns"](<column_number>)["FieldName"];

     

    Then get the field count for the recordset (there can be hidden ones - you can't assume the number of fields in the recordset is the same as the number of visible columns you can see on screen):

     

    var field_count = Obj_Grid["DataLink"]["DataSet"]["FieldList"]["Count"];

     

    Then iterate though the fields (fields are effectively columns in the underlying dataset) looking for a match with the field name found in the column in the GUI. The field name in the dataset can be found as follows:

     

    var field_name = Obj_Grid["DataLink"]["DataSource"]["DataSet"]["Fields"]["Fields"](<dataset field/column number>)["FullName"];

    When you hit a match for that name, vs the field name you got from the GUI column, you have got the right column/field in the dataset. (aka - you have found the field/column in the dataset which is providing the data for the column you actually see on screen)

     

    Columns in the GUI and fields in the dataset are VERY closely linked. The two combined effectively form what you see on the screen. But you have to almost treat them as two separate objects. They each have their own distinct set of controls and methods. They are all sorts of fun to work with!

     

    Fortunately for me, I have a couple of experienced Delphi developers on hand to help me out with the controls.

     

    Of course, if it's not a Delphi DB Grid you're dealing with, disregard all the above!

     

    :)