Hello: this is my first window app in .net and have been assigned to create a form that contains a datagridview.

I decided to leave it unbound, because I need it to populate in a slightly unconventional way.
I also created each column and set them as drop-downs in the designer.

I need to populate every 3rd row with a list of colors (pulled from my access db).
the row after this will always need to populate with a list of brightness values (also pulled from my access db).

Here's my code thus far:

When the form loads, I create a specified set of rows. There will always be a given amount of rows and columns..the user cannot add or remove...just edit.

Private Sub LoadDataGrid_New()
        ' Create an unbound DataGridView by declaring a column count.
        Dim Row0 As String = ""
        Dim Row1 As String = ""

        'Dim Row0 As String() = {""}
        'Dim Row1 As String() = {""}
        ''Dim Row2 As String() = {""}

        With Me.DataGridView1.Rows
            .Add(Row0)
            .Add(Row1)
            '.Add(Row2)
        End With

    End Sub

After, I make a stab at populating the first row w/ colors:

Sub PopulateColorRow()

        Dim myDB As New OleDb.OleDbConnection(sConnectionString)
        Dim myDA As New OleDb.OleDbDataAdapter("Select ColorId,Color from tblColors order by Color", myDB)
        Dim myDS As New Data.DataSet
        Dim cbn As DataGridViewComboBoxColumn = DirectCast(DataGridView1.Columns(0), DataGridViewComboBoxColumn)
        'Dim cbn1 As DataGridViewComboBoxColumn = DirectCast(Me.DataGridView1.Columns.Item(2), DataGridViewComboBoxColumn)


        'DataGridView1.Rows(0).Cells(0).Value = "COLOR"
        'DataGridView1.Rows(1).Cells(0).Value = "BRIGHTNESS"

        myDA.Fill(myDS, "tblColors")

        With cbn
            .DataSource = myDS.Tables("tblColors")
            .DisplayMember = "Color"
            .ValueMember = "ColorId"
        End With

        myDB.Close()

    End Sub

It complains when after it completes this...it doesn't like the line I highlighted above in green....if I change the column index to 1, it's happy, but it doesn't put data in the first column . The main issue however, is that I need to know how to tell it to populate my dropdowns every 3rd row..(for now, just trying to get it to work on the first row).

I would sure appreciate any input at all.
thanks,
proctor

Proctor, DataGridView doesn't allow to change type of Column once it defined.

you just try this code

DataGridView1.Columns.Remove(DataGridView1.Columns(0))
dim cbn as New DataGridViewComboBoxColumn
With cbn
.DataSource = myDS.Tables("tblColors")
.DisplayMember = "Color"
.ValueMember = "ColorId"
End With
DataGridView1.Columns.Insert(0, cbn)

Regards

Warun

Hello warun: thanks for your help...that worked!!!


Proctor

how bind the data in DataGridViewComboBoxColumn from database in vb.net using mysql database

Imports MySql.Data.MySqlClient
Public Class Form1
    Dim mcn As MySqlConnection
    Dim mcm As MySqlCommand
    Dim mda As New MySqlDataAdapter
    Dim mdt As New DataTable
    Dim mds As New DataSet
    Dim str As String
    Dim instance As DataGridViewComboBoxColumn
    Dim value As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        str = "server=localhost;uid=root;pwd=bss;database=cdetail"
        mcn = New MySqlConnection
        mcn.ConnectionString = str
        mcn.Open()
        FillGridView()

        End Sub

    Private Sub FillGridView()

        '--- Adding the values into the combo box through the List---
        Dim cmbOpt1 As DataGridViewComboBoxColumn = CType(DataGridView1.Columns("loc"), DataGridViewComboBoxColumn)
        Dim cmbOpt2 As DataGridViewComboBoxColumn = CType(DataGridView1.Columns("PRODUCT"), DataGridViewComboBoxColumn)
        Try

            str = "select Name,fullname from others order by Name"
            mda = New MySqlDataAdapter(str, mcn)
            Dim mdt1 As New DataTable
            mda.Fill(mdt1)
            'If loc.DataSource Is Nothing Then
            cmbOpt1.DataSource = mdt1
            cmbOpt1.DisplayMember = "Name"
            cmbOpt1.ValueMember = "fullname"
       
            Dim str2 As String
            Dim mda1 As New MySqlDataAdapter
            Dim mdt2 As New DataTable
            str2 = "select productname,fullname from product order by productname"
            mda = New MySqlDataAdapter(str2, mcn)
            mda.Fill(mdt2)
            cmbOpt2.DataSource = mdt2
            cmbOpt2.DisplayMember = "productname"
            cmbOpt2.ValueMember = "fullname"

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

End Class
This article has been dead for over six months. Start a new discussion instead.