Results 1 to 3 of 3

Thread: dropdown in a gridview ?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    10

    dropdown in a gridview ?

    Hi,

    i'm trying to get a dropdown in a filled datagrid, the code i have so far is :

    Code:
     Dim mycommand3 As New MySqlCommand
            Dim myadapter3 As New MySqlDataAdapter
            Dim sql As String
            Dim mydata3 As New DataTable
            sql = "select Lasnummer,diameter1 from lisl_gegevens_new where Isonummer ='" & isonr & "' order by lasnummer;"
            Connectdb()
            mycommand3.Connection = conn
            mycommand3.CommandText = sql
            myadapter3.SelectCommand = mycommand3
            myadapter3.Fill(mydata3)
            ListLas.lassen.DataSource = mydata3
    
            Dim dgvcolumn As New DataGridViewComboBoxColumn
    
            For a As Integer = 1 To 10
                dgvcolumn.Items.Add(CStr(a))
            Next a
    
            dgvcolumn = ListLas.lassen.Columns(1)
    On the last command i'm getting an error :

    Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxColumn' to type 'System.Windows.Forms.DataGridViewComboBoxColumn'.

    Could some explain to me how this is done correctly. The datagrid itself is filled correctly.

    Thanks in advance
    Erwin
    Last edited by etsoft; Dec 30th, 2007 at 08:28 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: dropdown in a gridview ?

    It would be in your best interests if you used the correct names for things in future. A DataGridView is NOT a GridView and it is NOT a DataGrid. It is a DataGridView and nothing else. A GridView is something else and so is a DataGrid. I ignored this thread because by the title it sounded like it related to a DropDownList in a GridView control, which would make it a Web application. If you'd said a ComboBox in a DataGridView then it would have been plain what it was actually about.

    As to your problem, if you want your grid to contain a combo box column then you have to add a combo box column to it. A text box column cannot magically become a combo box column because they are two different things. You need to actually create a DataGridViewComboBoxColumn object, add it to the DataGridView and then set its DataPropertyName to the name of the column in the grid's DataSource you want it to bind to. You can do this in code or at design time, but I'd recommend doing it in the designer. If you do choose to do it in code then it must be done BEFORE you bind the grid.

    If the DataTable you're binding contains a ParentID column then you'd set the column's DataPropertyName to "ParentID". Now, when you bind the DataTable to the grid, the ParentID column will be bound to your existing combo box column instead of a new text box column being created for it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    10

    Re: dropdown in a gridview ?

    your rigth of course, my apologies. I will try to get thsi right.
    I need to solve something else first, i will open a new thread.

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