Solved

DataGridView ComboBoxCell Editing Problem

Posted on 2007-04-09
8
369 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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