Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

DataGridView ComboBoxCell Editing Problem

Posted on 2007-04-09
8
368 Views
Last Modified: 2012-08-13
I need to be able to edit the contents of a datagridview combobox cell or select one of the items in the dropdown list. If I understand the following statement from MSDN,
(http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcell.aspx)
 I should be able to do it:

The DataGridViewComboBoxCell class is a specialized type of DataGridViewCell used to display a combo box control, which is an editing field combined with a list selection field. The currently selected DataGridViewComboBoxCell hosts a DataGridViewComboBoxEditingControl in which the user can change the cell's value, assuming the cell's ReadOnly property is set to false.

I have set each cell's ReadOnly value to false using the following code,but it still only allows me to select from the dropdown list.

            With dgvStrataBdry
                For I = 0 To .RowCount - 1
                    .CurrentCell = Me.dgvStrataBdry(3, I)
                    .CurrentCell.ReadOnly = False
                Next
            End With

Am I misunderstanding that it should allow editing or just doing it wrong.


0
Comment
Question by:rkulp
  • 6
  • 2
8 Comments
 
LVL 1

Author Comment

by:rkulp
ID: 18878684
I forgot to mention that the grid is unbound and all values are either programmatically set or entered by the user.
0
 
LVL 18

Accepted Solution

by:
Priest04 earned 500 total points
ID: 18879625
No, changing ReadOnly will only allow/deny user to choose item from combobox. In order to allow typing in DataGridViewComboBox cell, you need to do 2 things

1) In DataGridView EditingControlShowing event set combobox DropDownStyle to DropDown

CType(e.Control, ComboBox).DropDownStyle = ComboBoxStyle.DropDown

that will allow editing, and

2) you need to manually add this new value to items collection
0
 
LVL 1

Author Comment

by:rkulp
ID: 18881793
Priest04,

Works like a champ. Thanks.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 18

Expert Comment

by:Priest04
ID: 18882045
You are welcome ;)
0
 
LVL 1

Author Comment

by:rkulp
ID: 18883829
I spoke too quickly. Evidently I don't know how to properly address the column to change the value. I can enter the value in the cell (where I couldn't before) but when I leave the cell (which I neglected to do earler in my rush to get to jury duty) the value returns to one of the previously set items. Apparently I did not set the items correctly. Here is the code. Please show me the correct way.

        With dgvStrataBdry
            If e.ColumnIndex = 3 Then
                dgvStrataBdry.CurrentCell = Me.dgvStrataBdry(e.ColumnIndex, e.RowIndex)
                Select Case .CurrentCell.Value
                    Case "No"
                        AuditInfo.CensusFlag(e.RowIndex) = False
                        AuditInfo.NHh(e.RowIndex) = 0
                    Case "Yes"
                        AuditInfo.CensusFlag(e.RowIndex) = True
                        AuditInfo.NHh(e.RowIndex) = AuditInfo.Nh(e.RowIndex)
                    Case Else
                        AuditInfo.CensusFlag(e.RowIndex) = False
                        AuditInfo.NHh(e.RowIndex) = CType(.CurrentCell.Value, Integer)
                        Dim cbcol As DataGridViewComboBoxColumn
                        cbcol = dgvStrataBdry.Columns(3)
                        cbcol.Items.Add(.CurrentCell.Value)

                End Select
            End If
0
 
LVL 1

Author Comment

by:rkulp
ID: 18883851
The code above is in:

    Private Sub dgvStrataBdry_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvStrataBdry.CellValueChanged

Maybe that is not the correct place for it.
0
 
LVL 1

Author Comment

by:rkulp
ID: 18883997
I also have tried the following which I found elsewhere in E-E. It does not work.
   Dim cb As New DataGridViewComboBoxCell
       cb = DataGridView1(comboboxcolumnindex, row index)
       cb.items.add("xyz")

I don't want to artificially superimpose a text box on top of the cell to capture the information and then put its value in the items. I hope there is a better way. I'm just too dense to see it.
0
 
LVL 1

Author Comment

by:rkulp
ID: 18894083
This seems to work:

    Private Sub dgvStrataBdry_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvStrataBdry.EditingControlShowing
        If dgvStrataBdry.CurrentCellAddress.X = 3 Then
            CType(e.Control, ComboBox).DropDownStyle = ComboBoxStyle.DropDown
        End If
    End Sub
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question