Problem with TDBGrid and TDBDropDown

Posted by: isis on 7 August 2018, 6:15 am EST

    • Post Options:
    • Link

    Posted 7 August 2018, 6:15 am EST - Updated 29 September 2022, 2:56 am EST

    Hello,

    We are using ComponentOne TDBGrid Pro 8.0 (OLEDB) Version 8.0.20081.362 in a VB6 app.

    I am working on a new screen where we use several TDBDropDown controls to translate the codes of some fields. It is not the first time we use it, but now we are facing a new situation where we can not deal with.

    Suppose the following main table LOCAIS that is in TDBGRID1:

    LD_TagEquip

    LD_Local

    LD_MecDet

    LD_TipoMecDet

    These 2 last fields contain codes from the tables:

    TABMECDET: (linked to field LD_MecDet through TDBDropDownMD)

    MD_Codigo

    MD_Descricao

    TABTIPOSMECDET: (linked to field LD_TipoMecDet through TDBDropDownTD)

    TD_Codigo

    TD_Descricao

    TD_CodMecDet (dependent on the value of MD_Codigo)

    I can see the correct descriptions for the field LD_TipoMecDet when first feeding it, through this routine:

    Private Sub TDBGrid1_BeforeColEdit(ByVal ColIndex As Integer, ByVal KeyAscii As Integer, Cancel As Integer)
        If ColIndex = 13 Then
           cmd3.CommandText = "SELECT * FROM TabTiposMecDet WHERE TD_CodMecDet='" + TDBGrid1.Columns("LD_MecDet").Value + "' ORDER BY TD_Descricao"
           rsTD.Close
           rsTD.Open cmd3, , adOpenStatic, adLockReadOnly
           TDBDropDownTD.DataSource = rsTD
        End If
    End Sub
    

    The problem is when we close and reopen the form, so the column LD_TipoMecDet is not showing the descriptions because it depends on the content of the field LD_MecDet. (see attached image)

    I tried many routines in Form_Load, with no success.

    Should I use a specific event to put this routine, or another one?

    I am very grateful for any help.

    Isis

  • Posted 7 August 2018, 8:58 pm EST

    Hi,

    Can you please share a small stripped down application replicating the behavior? I would like to investigate it further.

    Also, share a dummy database to bind TrueDBGrid and TrueDBDropDown used in this project.

    ~Pragati

  • Posted 9 August 2018, 12:10 am EST

    Hello Pragati,

    I adapted the code to extract the troubled form to generate a small apllication for you.

    Attached the files you will need.

    Thanks in advance for your attention.

    Isis

    TesteTDBGrid.zip

  • Posted 12 August 2018, 5:08 pm EST - Updated 29 September 2022, 2:56 am EST

    Hi,

    Thank you for sharing the application.

    It was missing some PowerButton.ocx. However, I loaded the project without the file so as to reproduce the TDBGrid related problem. When I executed the application, it did show a screen as attached. Changing any value in any of the cells in the grid threw a runtime error.

    Can you please let me know:

    1. If it’s fine to try replicating the problem without PowerButton.ocx file? If it’s required, kindly share.
    2. What exact steps I should follow in your application to replicate the problem?

      This would help me in assisting you further at the earliest.

    Thanks,

    Pragati

  • Posted 14 August 2018, 3:54 am EST

    Hello Pragati,

    Sorry, I’ve forgotten to remove the reference to PowerButton.ocx. It is not needed and it is removed now from the project.

    To replicate the problem you don’t need to do anything, just open the application so you will see the column “Tipo do Mec. de Deter.” showing the codes instead of the descriptions, like the image of my first message. Now I turned the column “Elemento” invisible to help your visualization.

    If you get some runtine error, please let me know the description.

    Thanks for your attention and feel free to ask anything else.

    Isis

    TesteTDBGrid.zip

  • Posted 16 August 2018, 7:39 pm EST

    Hi,

    Thank you for sharing the modified project.

    In order to show Descriptions rather than codes in the column “Tipo do Mec. de Deter.”, please use the following code in Form’s Load event:

    
    cmd3.CommandText = "SELECT * FROM TabTiposMecDet WHERE TD_CodMecDet='" + TDBGrid1.Columns("LD_MecDet").Value + "' ORDER BY TD_Descricao"
    
    

    instead of the following (currently used in your application),

    
      cmd3.CommandText = "SELECT * FROM TabTiposMecDet WHERE TD_CodMecDet='??' ORDER BY TD_Descricao"
    
    

    ~Pragati

  • Posted 22 August 2018, 4:52 am EST

    Hello Pragati,

    I am very grateful for your attention, but this is not the solution, because when we use the suggested WHERE clause, it returns the code of the first “MecDet”, I mean, the value of LD_MecDet of the first record, what is not correct, because each record has a different value for this field. So the grid tries to show the decriptions based on this recordset, that is not valid for the other records, only for the first. So, sometimes we get no descriptions and sometimes a wrong description.

    I believe we need a cell-based code, but I don’t know which code to use, nor in which TDBGrid

    event…

    I tried the following codes:

    Private Sub TDBGrid1_BeforeColEdit(ByVal ColIndex As Integer, ByVal KeyAscii As Integer, Cancel As Integer)
        ' It works to show the correct recordset of TabTiposMecDet in TDBDropDownTD,
        ' but we see the correct description only when we click on the field to open the dropdown
        If ColIndex = 13 Then
           cmd3.CommandText = "SELECT * FROM TabTiposMecDet WHERE TD_CodMecDet='" + TDBGrid1.Columns("LD_MecDet").Value + "' ORDER BY TD_Descricao"
           rsTD.Close
           rsTD.Open cmd3, , adOpenStatic, adLockReadOnly
           TDBDropDownTD.DataSource = rsTD
        End If
    End Sub
    
    Private Sub TDBGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
        ' It is intended to show the corresponding description but it only works when we navigate through the columns and rows
        If TDBGrid1.Col = 13 Then
           cmd3.CommandText = "SELECT * FROM TabTiposMecDet WHERE TD_CodMecDet='" + TDBGrid1.Columns("LD_MecDet").Value + "' ORDER BY TD_Descricao"
           rsTD.Close
           rsTD.Open cmd3, , adOpenStatic, adLockReadOnly
           TDBDropDownTD.DataSource = rsTD
        End If
    End Sub
    

    Thanks for any help,

    Isis

  • Posted 26 August 2018, 7:08 pm EST

    When it comes to planning a trip to a place where one can enjoy historical sites along with contemporary creation Baltic States is the best. It is one of the most visited destinations in Europe and is pure beauty. Norlendatrip.com offers top-class quality Baltic tours package that one must not miss. Here you will get a chance to explore the history and can also enjoy some of the authentic cuisines of the city. To plan your trips reach us out now. Web - https://norlendatrip.com/tour/baltic-tours-and-travel/

  • Posted 26 August 2018, 8:01 pm EST

    Hi,

    I am discussing the problem with the developers. Will let you know once there is any information from them.

    ~Pragati

  • Posted 27 August 2018, 8:57 pm EST

    Hi,

    Your requirement is to display a description based on a code stored in a table separate from the the table stored in the grid, but based (at least in part) on one or more values stored in the table that is displayed in the grid.

    There are two ways of handling this in code with TDBGrid:

    1. if there is a relatively limited set of possible values for the translated column, generate a ValueItemsCollection containing all the translations and attach them to the appropriate column in the grid.

    2. if there is a large set of possible values for the translated column, then instead of adding a bound column, add an unbound column for the translation, and generate the translated value as needed in the UnboundColumnFetch event as it is needed.

    Thanks,

    Pragati

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels