Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 410
  • Last Modified:

Datagridview with a dropdown

I have a datagridview, and all the cells are textboxes. I would like to change one of the textboxes to a dropdown. How do I do that?

Dim adp As OleDb.OleDbDataAdapter
    Dim stTeksten As DataSet

    Private Sub Teksten_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strSQL As String
        strSQL = "SELECT ID, Taal, Nummer, Tekst FROM Tekst ORDER BY Nummer, Taal;"
        adp = New OleDb.OleDbDataAdapter(strSQL, GetConnection)
        stTeksten = New DataSet
        adp.Fill(stTeksten, "Teksten")
        Me.gridTeksten.DataSource = stTeksten.Tables("Teksten")
        Me.gridTeksten.Columns("ID").Visible = False
        'gridteksten.Columns(2).
    End Sub

    Private Sub SaveGrid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Dim cmdbuilder As New OleDb.OleDbCommandBuilder(adp)
        Dim i As Integer
        Try
            i = adp.Update(stTeksten, "Teksten")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

Open in new window

0
Frans_Truyens
Asked:
Frans_Truyens
  • 4
  • 3
1 Solution
 
me655321Commented:
You can't do exactly what you're asking for. Even if the data wasn't bound, I don't think you can have one combobox in a textbox column. What you could do is add a new DataGridViewComboBoxColumn bound to that table/field, but in that way, all the items in that column would be dropdowns.
0
 
Frans_TruyensAuthor Commented:
I mean, i have three columns. Two of them have to stay textboxes, and the third one has to be a dropdown. So all the items in the third column will be dropdowns.

Could you give me a code example? I am trying it, but i got to here:


Dim strSQL As String
        If gTaal = "FR" Then
            strSQL = "SELECT ID, Taal AS Langue, Nummer AS Numéro, Tekst AS Texte FROM Tekst ORDER BY Nummer, Taal;"
        Else
            strSQL = "SELECT ID, Taal, Nummer, Tekst FROM Tekst ORDER BY Nummer, Taal;"
        End If
        adp = New OleDb.OleDbDataAdapter(strSQL, GetConnection)
        stTeksten = New DataSet
        adp.Fill(stTeksten, "Teksten")
        Me.gridTeksten.DataSource = stTeksten.Tables("Teksten")
        Me.gridTeksten.Columns("ID").Visible = False
        Dim colTest As New DataGridViewComboBoxColumn  'DataGridViewColumn
        colTest.ValueMember = "Taal"
        colTest.Items.Add("NL")
        colTest.Items.Add("FR")
        Me.gridTeksten.Columns.Add(colTest)
0
 
me655321Commented:
This should work...

        Dim colTest As New DataGridViewComboBoxColumn  'DataGridViewColumn
        colTest.DataPropertyName = "Taal"
        colTest.ValueMember = "Taal"
        colTest.DisplayMember = "Taal"
        Me.gridTeksten.Columns.Add(colTest)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Frans_TruyensAuthor Commented:
Thanks. I played a little bit with the code. But it still does not work. It shows the column 'Taal' two times. Once because it is in the dataset, and once because I add it.

I want ONLY to see the Language combo ONCE. How do I do that? You can modify the code below.


        Dim strSQL As String
        If gTaal = "FR" Then
            strSQL = "SELECT ID, Taal AS Langue, Nummer AS Numéro, Tekst AS Texte FROM Tekst ORDER BY Nummer, Taal;"
        Else
            strSQL = "SELECT ID, Taal, Nummer, Tekst FROM Tekst ORDER BY Nummer, Taal;"
        End If
        adp = New OleDb.OleDbDataAdapter(strSQL, GetConnection)
        stTeksten = New DataSet
        adp.Fill(stTeksten, "Teksten")
        Me.gridTeksten.DataSource = stTeksten.Tables("Teksten")

        Dim colTest1 As New DataGridViewComboBoxColumn
        colTest1.DataPropertyName = "Taal"
        colTest1.ValueMember = "Taal"
        colTest1.DisplayMember = "Taal"
        colTest1.HeaderText = "Taal"
        colTest1.Items.Add("NL")
        colTest1.Items.Add("FR")
        Me.gridTeksten.Columns.Add(colTest1)

Open in new window

0
 
me655321Commented:
You can either do:
        DataGridView1.Columns.Remove("Taal")
or
        DataGridView1.Columns("Taal").Visible = False

to remove the original column. Also, I forgot to have you actually set the data to the new column...so the code should be like this:
        Dim colTest1 As New DataGridViewComboBoxColumn
        colTest1.DataSource = stTeksten.Tables("Teksten")
        colTest1.DataPropertyName = "Taal"
        colTest1.ValueMember = "Taal"
        colTest1.DisplayMember = "Taal"
        colTest1.HeaderText = "Taal"
        Me.gridTeksten.Columns.Add(colTest1)

I also took out the item adding. This method will automatically populate the dropdowns from the dataset so you don't/can't add items manually.
0
 
me655321Commented:
I'm sorry, ignore what I just wrote....this is what you want:


Dim strSQL As String 
        If gTaal = "FR" Then 
            strSQL = "SELECT ID, Taal AS Langue, Nummer AS Numéro, Tekst AS Texte FROM Tekst ORDER BY Nummer, Taal;" 
        Else 
            strSQL = "SELECT ID, Taal, Nummer, Tekst FROM Tekst ORDER BY Nummer, Taal;" 
        End If 
        adp = New OleDb.OleDbDataAdapter(strSQL, GetConnection) 
        stTeksten = New DataSet 
        adp.Fill(stTeksten, "Teksten") 
        Me.gridTeksten.DataSource = stTeksten.Tables("Teksten") 
 
        DataGridView1.Columns.Remove("Taal")
        Dim colTest1 As New DataGridViewComboBoxColumn 
        colTest1.Items.Add("NL") 
        colTest1.Items.Add("FR") 
        colTest1.DataPropertyName = "Taal" 
        colTest1.Name = "Taal"
        Me.gridTeksten.Columns.Add(colTest1)

Open in new window

0
 
Frans_TruyensAuthor Commented:
Thanks a lot.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now