none
Populate Datagridviewcombobox (With Out SQL or other dataconnection) How Do I? RRS feed

  • Question

  • So I have been pulling my hair out for a few hours.

    I have an already Filled out Datagridview. Done progmaticly. As I am 

    not using any SQL or datatable source. Columns can vary from 3 to as much as it can handle.

    And all I want is one single combobox in my datagrid. That can float depending on the data I am giving it. But it will also stay in row 0, just the column will change. Any cell under the combobox are left as stand textbox's. The user will only need to adjust the value in the single combobox.

    I can use this code with no errors.

    DIM CB as Datagridviewcombobox

    cb.items.insert(0,"Value 1")

    cb.items.add ("Value 2") 

    (and so on)

    Form1.Datagridview1(1,0) = CB

    The error is, that there is nothing in the combobox. I have also try linking the datasource to a new list of string, that I have populated by hand. And still will not show anything. And the only help I can find, is the above or linking what I want to a datatable.

    Any help would be Great :)

    Wednesday, June 19, 2019 2:31 PM

Answers

  • Hello,

    Here is a very simple example.

    Public Class Form1
        Private _comboColumnName As String = "C1"
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim source = {"Add", "Subtract", "Divide", "Multiply"}
    
            Dim dataGridViewCellStyle1 = New DataGridViewCellStyle With
                    {
                        .Alignment = DataGridViewContentAlignment.MiddleLeft
                    }
    
            Dim column1 = New DataGridViewComboBoxColumn With
                    {
                        .DataSource = source,
                        .DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing,
                        .DefaultCellStyle = dataGridViewCellStyle1,
                        .Name = _comboColumnName, .HeaderText = "Demo",
                        .SortMode = DataGridViewColumnSortMode.NotSortable
                    }
    
            Dim column2 = New DataGridViewCheckBoxColumn With {.Name = "C2", .HeaderText = "Col 2"}
    
            Dim column3 = New DataGridViewTextBoxColumn With {.Name = "C3", .HeaderText = "Col 3"}
    
            DataGridView1.Columns.AddRange(column1, column2, column3)
    
            DataGridView1.Rows.Add("Divide", True, "Karen")
            DataGridView1.Rows.Add("Add", False, "Kevin")
            DataGridView1.Rows.Add("Subtract", True, "Anne")
            DataGridView1.Rows.Add("Add", True, "Joe")
            DataGridView1.Rows.Add("Multiply", True, "Jean")
        End Sub
    End Class

    Helper extension

    Public Module Extensions
        <System.Runtime.CompilerServices.Extension>
        Public Function IsComboBoxCell(ByVal sender As DataGridViewCell) As Boolean
            Dim result As Boolean = False
    
            If sender.EditType IsNot Nothing Then
                If sender.EditType Is GetType(DataGridViewComboBoxEditingControl) Then
                    result = True
                End If
            End If
            Return result
    
        End Function
    End Module


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Daniel_AUS Thursday, June 20, 2019 4:20 AM
    Wednesday, June 19, 2019 3:10 PM
    Moderator

All replies

  • Hello,

    Here is a very simple example.

    Public Class Form1
        Private _comboColumnName As String = "C1"
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim source = {"Add", "Subtract", "Divide", "Multiply"}
    
            Dim dataGridViewCellStyle1 = New DataGridViewCellStyle With
                    {
                        .Alignment = DataGridViewContentAlignment.MiddleLeft
                    }
    
            Dim column1 = New DataGridViewComboBoxColumn With
                    {
                        .DataSource = source,
                        .DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing,
                        .DefaultCellStyle = dataGridViewCellStyle1,
                        .Name = _comboColumnName, .HeaderText = "Demo",
                        .SortMode = DataGridViewColumnSortMode.NotSortable
                    }
    
            Dim column2 = New DataGridViewCheckBoxColumn With {.Name = "C2", .HeaderText = "Col 2"}
    
            Dim column3 = New DataGridViewTextBoxColumn With {.Name = "C3", .HeaderText = "Col 3"}
    
            DataGridView1.Columns.AddRange(column1, column2, column3)
    
            DataGridView1.Rows.Add("Divide", True, "Karen")
            DataGridView1.Rows.Add("Add", False, "Kevin")
            DataGridView1.Rows.Add("Subtract", True, "Anne")
            DataGridView1.Rows.Add("Add", True, "Joe")
            DataGridView1.Rows.Add("Multiply", True, "Jean")
        End Sub
    End Class

    Helper extension

    Public Module Extensions
        <System.Runtime.CompilerServices.Extension>
        Public Function IsComboBoxCell(ByVal sender As DataGridViewCell) As Boolean
            Dim result As Boolean = False
    
            If sender.EditType IsNot Nothing Then
                If sender.EditType Is GetType(DataGridViewComboBoxEditingControl) Then
                    result = True
                End If
            End If
            Return result
    
        End Function
    End Module


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Daniel_AUS Thursday, June 20, 2019 4:20 AM
    Wednesday, June 19, 2019 3:10 PM
    Moderator
  • Thank you very much :)
    Thursday, June 20, 2019 4:20 AM
  • Thank you very much :)
    Your welcome.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, June 20, 2019 10:03 AM
    Moderator