?
Solved

DataGridView ComboBoxCell Editing Problem

Posted on 2007-04-09
8
Medium Priority
?
371 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
[X]
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
  • 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 2000 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 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