Problem to find column in C1Combo with IndexOf()

Posted by: thomas on 17 June 2020, 4:27 pm EST

    • Post Options:
    • Link

    Posted 17 June 2020, 4:27 pm EST

    Hi

    Can’t get this to work:

    Dim dc As New C1DataColumn

    dc.DataField = “Kflag”

    Dim iCol = C1Combo1.Columns.IndexOf(dc)

    iCol is -1

    This works:

    Dim iCol As Integer = -1

    For i = 0 To C1Combo1.Columns.Count - 1

    If C1Combo1.Columns(i).DataField = “Kflag” Then

    iCol = i

    Exit For

    End If

    Next

    iCol is correct column.

    (Datasource is datatable)

    Must say I’m not sure about the use of C1DataColumn. In other controls it’s possible to use string in IndexOf, but in C1Combo it need to be C1DataColumn.

    I’m using ComponentOne Studio for WinForms (.NET 4.0) 4.0.20183.343

  • Posted 18 June 2020, 7:45 am EST

    Hi Thomas,

    The code snippet you used is creating a new instance of C1DataColumn which is not the part of C1Combo1. To get the correct column index use the code snippet given below:

    Dim iCol = C1Combo1.Columns.IndexOf(C1Combo1.Columns("Kflag"))
    
    

    Regards,

    Prabhat Sharma.

  • Posted 18 June 2020, 8:37 am EST

    Hi Prabhat!

    Great, thank you! :slight_smile:

    Now I only need to check if the column exists. In other C1-components it world return -1, but not in this one. How can I check that?

    Regards

    Thomas Lindgren

  • Posted 19 June 2020, 12:31 am EST

    Hi Thomas,

    You can use the code snippet given below to check. whether the specified column exists or not:

     Dim colExist As Boolean = False
            For i = 0 To C1Combo1.Columns.Count - 1
                If C1Combo1.Columns(i).DataField = "Field Name" Then
                    colExist = True
                    Exit For
                End If
            Next
            If colExist Then
                Console.WriteLine("Exists")
            Else
                Console.WriteLine("Does  not Exist")
            End If
    

    Regards,

    Prabhat Sharma.

Need extra support?

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

Learn More

Forum Channels