Solved

DataGridView ComboBoxCell Editing Problem

Posted on 2007-04-09
8
363 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
 
LVL 18

Expert Comment

by:Priest04
ID: 18882045
You are welcome ;)
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now