Home All Groups Group Topic Archive Search About

DataGridView... defaults to DataGridViewTextBoxColumn

Author
12 Jan 2006 6:08 PM
johnb41
When you programmatically bind data to a DataGridView (VS2005), the
resulting columns are DataGridViewTextBoxColumns.  Instead of that, i'd
like it to be a DataGridViewComboBoxColumn.  How can I do this?

Thanks for the help!
John

Author
12 Jan 2006 9:41 PM
Ken Tucker [MVP]
Hi,

        Here is a quick example.


Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        Dim dt As New DataTable("Names")
        dt.Columns.Add("Name")
        dt.Columns.Add("State")
        dt.LoadDataRow(New Object() {"Ken Tucker", "Florida"}, True)
        dt.LoadDataRow(New Object() {"Cor Ligthert", "Netherlands"}, True)
        dt.LoadDataRow(New Object() {"Terry Burns", "United Kingdom"}, True)
        dt.LoadDataRow(New Object() {"Armin Zignler", "Germany"}, True)
        dt.LoadDataRow(New Object() {"Herfried K. Wagner", "Austria"}, True)
        dt.LoadDataRow(New Object() {"Jay B Harlow", "New York"}, True)
        DataGridView1.DataSource = dt
        DataGridView1.AllowUserToAddRows = False

        DataGridView1.Columns.Remove("State")

        Dim dgvCombo As New DataGridViewComboBoxColumn
        With dgvCombo
            .Width = 150
            .Items.Add("Florida")
            .Items.Add("Netherlands")
            .Items.Add("United Kingdom")
            .Items.Add("Germany")
            .Items.Add("Austria")
            .Items.Add("New York")
            .DataPropertyName = "State"
            .HeaderText = "State"
        End With

        DataGridView1.Columns.Add(dgvCombo)
    End Sub

Ken
------------------
Show quoteHide quote
"johnb41" <jsbuchm***@gmail.com> wrote in message
news:1137089282.649658.79170@g49g2000cwa.googlegroups.com...
> When you programmatically bind data to a DataGridView (VS2005), the
> resulting columns are DataGridViewTextBoxColumns.  Instead of that, i'd
> like it to be a DataGridViewComboBoxColumn.  How can I do this?
>
> Thanks for the help!
> John
>
Author
13 Jan 2006 4:47 AM
johnb41
Looks interesting, Ken. This looks like it will work for me.  Thanks
very much!

John