Editing Text in a DataGridViewComboBoxColumn
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 30

Thread: Editing Text in a DataGridViewComboBoxColumn

Hybrid View

  1. #1
    Join Date
    Dec 2002
    Location
    Tenby, Wales
    Posts
    277

    Editing Text in a DataGridViewComboBoxColumn

    Hi guys it's me again!

    I have a bit of a dilemma - in the ComboBoxColumn, I have added various numbers from 0 to 11. I need people to be able to type into the ComboBoxes in that column if the Combobox doesn't contain a value they need.

    I realise that I have to change the ComboBoxStyle to DropDown but am not sure how to. Also, for each row in the ComboBox, I believe I have to add the item that the user typed to the ComboBox list items for it to save but am also unsure how to do it.

    Any ideas?

    Cheers
    Visual Basic 2005 ver. 8.0.50727.867

  2. #2
    Join Date
    Dec 2002
    Location
    Tenby, Wales
    Posts
    277

    Re: Editing Text in a DataGridViewComboBoxColumn

    Right, I now have the ComboBoxStyle changed to DropDown which allows the user to type into the combobox using the following:
    Code:
        Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
            If DataGridView1.CurrentCellAddress.X = Description.Index Then
                Dim cbo As ComboBox = CType(e.Control, ComboBox)
                cbo.DropDownStyle = ComboBoxStyle.DropDown
            End If
        End Sub
    All I need now is each time the user changes to the next ComboBox in the column, the text typed into the previous one is saved into that list. Would something like this code I found on the net be along the right lines?:
    Code:
    Public Sub DataGridView1_CellValidating(ByVal e As DataGridViewCellValidatingEventArgs)
            ' Adds typed text to the ComboBox items
            Dim comboBoxColumn As DataGridViewComboBoxColumn
            If (e.ColumnIndex = comboBoxColumn.Index) Then
                If (comboBoxColumn.Items.Contains(e.FormattedValue)) Then
                    comboBoxColumn.Items.Add(e.FormattedValue)
                End If
            End If
        End Sub
    It doesn't seem to do anything though!

    Anyone?
    Last edited by brjames32; July 31st, 2008 at 05:59 AM.
    Visual Basic 2005 ver. 8.0.50727.867

  3. #3
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: Editing Text in a DataGridViewComboBoxColumn

    Hello Again!
    My solution to this would go something like this (after adding a "New" option to the combobox; also, I'm not using the proper table, so the column index and things may need changing)
    Code:
            Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
                    If e.ColumnIndex = 3 Then
                            If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString() = "New" Then
                                    Dim ComboCol As DataGridViewComboBoxColumn = CType(DataGridView1.Columns(3), DataGridViewComboBoxColumn)
                                    ComboCol.Items.Insert(ComboCol.Items.Count - 1, InputBox("New Item"))
                                    DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = ComboCol.Items(ComboCol.Items.Count - 2)
                            End If
                    End If
            End Sub
    You can even save added values, as DataGridViewComboBoxColumn is a valid datatype for the My.Settings object
    Last edited by javajawa; July 31st, 2008 at 06:18 AM.
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  4. #4
    Join Date
    Dec 2002
    Location
    Tenby, Wales
    Posts
    277

    Re: Editing Text in a DataGridViewComboBoxColumn

    Hiya

    Cheers for that - will give it a go and let ya know how I get on!
    Visual Basic 2005 ver. 8.0.50727.867

  5. #5
    Join Date
    Dec 2002
    Location
    Tenby, Wales
    Posts
    277

    Re: Editing Text in a DataGridViewComboBoxColumn

    That is definitely more along the lines!

    What I really need it to be able to do though is when the user clicks on the combobox cell, they should be able to type into it and that value should be saved instead of having to type into an input box if that is possible?
    Visual Basic 2005 ver. 8.0.50727.867

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,092

    Re: Editing Text in a DataGridViewComboBoxColumn

    Perhaps if you could use the DataGridView's EditingControlShowing event something like this :
    Code:
        Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
            If TypeOf e.Control Is ComboBox Then
                Dim cb As ComboBox = TryCast(e.Control, ComboBox)
                cb.DropDownStyle = ComboBoxStyle.DropDown
                cb.Items.Insert(cb.Items.Count - 1, cb.Text)
            End If
        End Sub
    This allows you to type a value into that particular cell. It may not be 100% complete, but will put you on the right track

  7. #7
    Join Date
    May 2009
    Posts
    2

    Editing Text in a DataGridViewComboBoxColumn

    hi all.

    i hav added a datagricview into winform. i hav added 2 combobx column into that datagrid view.
    when i click the firsr combox column the second combobox column value should changed

    this is m y reqiurment was. my first combox column is Itemid and second combobx column is Itemdescription.

    as my reqirment when i chaged the itemid column the second column itemdescription should be changed.

    some one pls help mw for this.

    thnxs.
    Rishad...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center