Results 1 to 2 of 2

Thread: [RESOLVED] Add a row to an unbound grid with a combobox.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Resolved [RESOLVED] Add a row to an unbound grid with a combobox.

    Hello.

    I have an unbound DataGridView that I am trying to fill with data, but am having a problem with the combobox column. I create the columns like this:
    Code:
       Private Sub SetGridColumns()
    
            Dim intColID As Integer
            Dim chkCol As DataGridViewCheckBoxColumn
            Dim cmbCol As DataGridViewComboBoxColumn
            Dim Table As DB.Tables.Table
    
    
            With dgvFields
                intColID = .Columns.Add("ID", "ID")
                .Columns(intColID).Width = 0
                intColID = .Columns.Add("Name", "Name")
                .Columns(intColID).Width = My.Settings.FieldNameCol
                intColID = .Columns.Add("DataType", "DataType")
                .Columns(intColID).Width = My.Settings.FieldDataTypeCol
                chkCol = New DataGridViewCheckBoxColumn
                With chkCol
                    .Name = "Req"
                    .Width = My.Settings.FieldNullabelCol
                End With
                .Columns.Add(chkCol)
                intColID = .Columns.Add("5.2 Table Name", "TableName52")
                .Columns(intColID).Width = My.Settings.Field52TableCol
                intColID = .Columns.Add("5.2 Field Name", "FieldName52")
                .Columns(intColID).Width = My.Settings.Field52FieldCol
                intColID = .Columns.Add("Key", "Key")
                .Columns(intColID).Width = My.Settings.FieldPPkeyCol
                cmbCol = New DataGridViewComboBoxColumn
                With cmbCol
                    .DropDownWidth = My.Settings.FieldPPkeyTableCol
                    .HeaderText = "Parent Table"
                    .Name = "ParentTable"
                    .Width = My.Settings.FieldPPkeyTableCol
                    For Each Table In mcolTables
                        .Items.Add(Table)
                    Next
                End With
                .Columns.Add(cmbCol)
                intColID = .Columns.Add("Parent Field", "ParentField")
                .Columns(intColID).Width = My.Settings.FieldPPkeyFieldCol
            End With
    
        End Sub
    That works, and I have my columns like I want them, but I don't know how to add a row AND select the appropriate item in the combobox. I am adding items like this:

    Code:
            Dim strDataType As String
            Dim strKey As String = Nothing
            Dim strTable As String
            Dim strFieldRow As String()
            Dim colFields As DB.Tables.Table.Fields
            Dim Field As DB.Tables.Table.Fields.Field
    
    
            strTable = cboTables.Text
    
            If strTable <> String.Empty Then
                With mcolTables(strTable)
                    txtDescription.Text = .Description
                    colFields = .FieldsCollection
                End With
    
                dgvFields.Rows.Clear()
                ReDim strFieldRow(8)
    
                For Each Field In colFields
                    With Field
                        strDataType = .DBDataType
                        If .RequiresLength Then
                            strDataType &= "(" & .DataLength.ToString & ")"
                        End If
                        If .FKey And .PKey Then
                            strKey = "Both"
                        ElseIf .PKey Then
                            strKey = "Primary"
                        ElseIf .FKey Then
                            strKey = "Foriegn"
                        Else
                            strKey = String.Empty
                        End If
                        strFieldRow(eGridFieldColumns.ID) = .ID
                        strFieldRow(eGridFieldColumns.Name) = .Name
                        strFieldRow(eGridFieldColumns.DataType) = strDataType
                        strFieldRow(eGridFieldColumns.Required) = .Required.ToString
                        strFieldRow(eGridFieldColumns.Table52) = .TableName52
                        strFieldRow(eGridFieldColumns.Field52) = .Name52
                        strFieldRow(eGridFieldColumns.Key) = strKey
                        'How to get this to work?
                        strFieldRow(eGridFieldColumns.KeyTable) = .FKeyParentTable
                        strFieldRow(eGridFieldColumns.KeyField) = .FKeyParentField
    
                        dgvFields.Rows.Add(strFieldRow)
                        dgvFields.Rows(dgvFields.Rows.Count - 1).Cells(eGridFieldColumns.ID).ReadOnly = True
    
                    End With
    
                Next
            End If
    I can't figure out how to set the combobox column. And to top it off, since I changed the column from a text column to a combobox column, the grid is throwing exceptions, although it doesn't tell me what's wrong, it just tells me to handle the dataerror event, and all that I can get from that is e.Context = 3 and I can't find out what that means. Probably a type mis-match, but I can't find the proper way do select from the combobox. The rows add OK, but when I scroll the combobox column into view the first item is selected then the errors get thrown.

    Any ideas?
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Add a row to an unbound grid with a combobox.

    I found the problem. I had to change the way I fill the combobox. I wanted to use the objects to eliminate doing a lookup to get the primary key, but that doesn't work. When I changed the code as follows it worked:
    Code:
                    For Each Table In mcolTables
                        .Items.Add(Table.Name)
                    Next
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width