datagridviewcomboboxcolumn with datasource issue?

Posted by Sarrrva on Stack Overflow See other posts from Stack Overflow or by Sarrrva
Published on 2012-12-19T04:47:35Z Indexed on 2012/12/19 5:03 UTC
Read the original article Hit count: 174

Filed under:

i have some propblem in datagridviewcombobocolumn with custom datasource property in vb.net. when i add datasource it does not populate in datagridview combobox column it giving nothing.. any one please help me out from this problem...

code comboboxcell:

 Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
        ' Set the value of the editing control to the current cell value.
        MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)


        Dim ctl As ComboEditingControl = CType(DataGridView.EditingControl, ComboEditingControl)

        ctl.DropDownStyle = ComboBoxStyle.DropDown
        ctl.AutoCompleteSource = AutoCompleteSource.ListItems
        ctl.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest



        If (Me.DataGridView.Rows(rowIndex).Cells(0).Value <> Nothing) Then

            Dim GetValueFromRowToUseForBuildingCombo As String = Me.DataGridView.Rows(rowIndex).Cells(0).Value.ToString()

            ctl.Items.Clear()

            Dim dt As New DataTable()
            Try
                dt = TryCast(DirectCast(Me.DataGridView.Columns(ColumnIndex), ComboColumn).DataSource, DataTable)
            Catch ex As Exception
                MsgBox("error")
            End Try

            If (dt Is Nothing) Then


                ctl.Items.Add("")
            Else
                Dim thing As DataRow
                For Each thing In dt.Rows
                    ctl.Items.Add(thing(0).ToString)
                Next

            End If


            If Me.Value Is Nothing Then
                ctl.SelectedIndex = -1
            Else
                ctl.SelectedItem = Me.Value
            End If
            ctl.EditingControlDataGridView = Me.DataGridView
        End If
    End Sub

from code:

Dim widgets As New WidgetDataHandler
        Dim obj = widgets.GetAllWigetTypes()

        Dim dt As New DataTable
        Dim ListofmyObjects As New List(Of widget_types)(obj)
        Dim objList As New cObjectToTable(Of widget_types)(ListofmyObjects)


        dt = objList.GetTable()

        Dim obj1
        For Each obj1 In obj
            blPersons.Add(obj1)
        Next


        Dim col1 As New DataGridViewTextBoxColumn
        col1.DisplayIndex = 0
        col1.DataPropertyName = "Id"
        col1.HeaderText = "Id"

        dgvi00.Columns.Add(col1)



        Dim col2 As New ComboColumn
        col2.DisplayIndex = 1
        col2.SortMode = DataGridViewColumnSortMode.Automatic
        col2.HeaderText = "Name"
        col2.DataPropertyName = "Name"
        col2.ToolTipText = "Select something from my combo"

        Dim dst As New DataSet
        'Dim dt1 As New DataTable

        'dt1.Columns.Add(col2.HeaderText)
        'For Each thing In dt.Rows
        '    MsgBox(thing(1).ToString)
        '    dt1.Rows.Add(thing(1).ToString)
        'Next

        dst.Tables.Add(dt)
        col2.DataSource = dst.Tables(0)
        col2.DisplayMember = "Name"
        Me.dgvi00.Columns.AddRange(col2)
        dgvi00.DataSource = blPersons.BindingSource



        'setup the bindings for the binding navigator
        Dim bn As New _365_Media_Library.BindingNavigatorWithFilter
        bn.Dock = DockStyle.Bottom
        bn.GripStyle = ToolStripGripStyle.Hidden
        Me.Controls.Add(bn)
        bn.BindingSource = blPersons.BindingSource

note :

its working good in standalone application

regards and thanks sarva

© Stack Overflow or respective owner

Related posts about vb.net