datagridviewcomboboxcolumn with selectable items and selectedindex based on database

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
I'm working on a datagridview with a comboboxcolumn and normal columns.

The datasource is a mysql database.
Most of the things are working, I am able to create a comboboxcolumn, but the column is created at the end of the column automatically add when loaded the form. A column (the first one displayed) is a column which displays the group a user is in, momentarily it is shown as text cell, but it should be the combobox with standard options to select and it should display the selecteditem equal to the value of the record in the database.

Here the code I already have (the xml file as displayed in the code is where the items of the combobox are stored in):
VB.NET:
Private Sub Addressbook2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        SQL = "SELECT * FROM email"
        Try
            con.Open()
            Try
                Dim dgvcombo As New DataGridViewComboBoxColumn
                mycommand.Connection = con
                mycommand.CommandText = SQL
                myadapter.SelectCommand = mycommand
                myadapter.Fill(mydataset, "email")
                mydata = mydataset.Tables("email")
                DataGridViewX1.DataSource = mydata
                Me.DataGridViewX1.Columns("Id").Visible = False
                Dim fileI As FileInfo = New FileInfo(Application.StartupPath + "\groups.xml")
                If fileI.Exists Then
                    Dim doc As XDocument = XDocument.Load(Application.StartupPath + "\groups.xml")
                    dgvcombo.Items.Add("Select Group")
                    For Each element As XElement In doc.Descendants("Group")
                        dgvcombo.Items.Add(element.Value)
                    Next
                Else
                    MessageBox.Show("File not found!")
                End If
                dgvcombo.HeaderText = "Group"
                DataGridViewX1.Columns.Add(dgvcombo)
                Me.DataGridViewX1.Columns(1).Width = 100
                ButtonItem2.Checked = True
            Catch ex As Exception
                MsgBox("There was an error reading from the database: " & ex.Message)
            End Try
        Catch myex As MySqlException
            MessageBox.Show("Error connecting to the database: " & myex.Message)
        Finally
            If con.State <> ConnectionState.Closed Then con.Close()
            LoadXML()
        End Try
    End Sub

Thanks for any help.
 

Latest posts

Back
Top