CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    VB 2005 DataGridView : How Do I Work With The DGV Combobox Column?

    Q: How do I add a ComboboxColumn at design time?

    A: After you have added the dataGridView to your form, it should give you a little screen where you can select Add Columns,

    and specify the name, and Type. You could also at any time, just right click the Datagridview, select Add Columns, and

    select all the necessary options there.

    Q: How do I add a Combobox column at run time?

    A: All you need to do is to declare a DataGridViewComboBoxColumn object, set the desited properties, and add it to the

    DataGridView's Columns collection. Here is a small example :
    Code:
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim NewColumn As New DataGridViewComboBoxColumn() 'Declare new DGV CC
    
            With NewColumn 'Set Properties
                .DataPropertyName = "NewColumn" 'Name
                .HeaderText = "New Column" 'Heading
                .DropDownWidth = 160 'Width Of DropDown Box
                .Width = 90 'Display Width
                .MaxDropDownItems = 5 'How Many Items To Drop Down At A Time
                .FlatStyle = FlatStyle.Flat 'Appearance
                .Items.Add("1") 'Add Some Text Items
                .Items.Add("2")
                .Items.Add("3")
                .Items.Add("4")
                .Items.Add("5")
                .Items.Add("6")
                .Items.Add("7")
                .Items.Add("8")
                .Items.Add("9")
                .Items.Add("10")
            End With
            DataGridView1.Columns.Add(NewColumn) 'Add The Column
    
        End Sub
    Once you have added this code, you can run your project and will notice that the new column has been added at the back of the

    datagridview. You can select any of the items we've added from the list.

    Q: How can I allow the user to type text into the newly created Comboboxcolumn, and persist it?

    A: All You need is the DataGridView's EditingControlShowing event ( To allow for data entry ), and the DataGridView's

    CellValidating event, to ensure persistance of the typed value. Add the following code :
    Code:
        Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As 
    
    System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
                'Create a New ComboBoxColumn Object, And Cast The dataGridView's Column To That
                Dim comboBoxColumn As DataGridViewComboBoxColumn = CType(DataGridView1.Columns(5), DataGridViewComboBoxColumn)
                'If In ComboBoxColumn
                If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
                    If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
                        'Add The Text Entered By The User
                        comboBoxColumn.Items.Add(e.FormattedValue)
                        'Make Sure Value Stays Displayed ( May HAve To Enter Value Twice )
                        DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 
    
    comboBoxColumn.Items(comboBoxColumn.Items.Count - 1)
                    End If
                End If
        End Sub
    
        Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As 
    
    System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
            'If In ComboBox Column
            If (DataGridView1.CurrentCellAddress.X = DataGridView1.Columns(5).DisplayIndex) Then
                'Cast To Normal ComboBox
                Dim cb As ComboBox = CType(e.Control, ComboBox)
                If (cb IsNot Nothing) Then
                    'Change Style To DropDown, To Allow For Data Entry
                    cb.DropDownStyle = ComboBoxStyle.DropDown
                End If
    
            End If
        End Sub
    I am attaching a working sample with this post.

    Q: Where can I find a more detailed discussion about this topic?

    A:
    DataGridViewComboBox problem!

    DataGirdViewComboBox saving problem

    Editing Text in a DataGridViewComboBoxColumn
    Attached Files Attached Files

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

Featured