We help IT Professionals succeed at work.

vb.net 2010 DataGridViewComboCox column problems

rcblevins
rcblevins asked
on
Medium Priority
667 Views
Last Modified: 2012-05-11
Hello All,

I just posted another question where I was having a problem and thank CodeCruiser for answering. I have another problem now.  I have a datagridview that has several datagridviewcombobox columns.  I manually configure the columns and then set datasource at runtime.  Each of the comboxbox columns have the numbers 0 to 10 defined in the items collection.  The database would return a number between 0 and 10 to display in that column when the database source is set.  The list drops down and I can select an item, but when I go to a different cell the value I selected goes away and the value that the datasource provided returns.  Does anyone have any suggestions?  Thanks for any and all help.
Comment
Watch Question

John (Yiannis) ToutountzoglouInstructor Multiengine Pilot

Commented:
Are you using the same Bindingsource?
If so ,when you are selecting a value from one combobox the other the others turn to the same value having the same bindingsource position ...define different bindingsource for each combobox..

Author

Commented:
Sorry, I probably didn't explain myself well.  For example: when the datagridview is loaded the combobox cell will display a value of one.  When I click on the dropdown and change the value to 2, it shows that it selected the new value, but when I click off of the cell and cell loses focus, then the cell value changes back to 0 which is what it was when the datagridview was loaded.  Please let me know if that did not clarify the problem.  Thanks for your help.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
If I remember correctly, you said in previous question the grid is readonly right?

Author

Commented:
Sorry, no the grid is not readonly and the column is not readonly.  Could this have something to do with the fact that I manually input the list items and that I was getting the system.argumentexception from the datagrid?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
No that should not make a difference.
John (Yiannis) ToutountzoglouInstructor Multiengine Pilot

Commented:
I can not imagine where must be your problem ...
a very simple example ...(that works properly) is here
http://vb.net-informations.com/datagridview/vb.net_datagridview_combobox.htm

Author

Commented:
Hello,

I thought this may have been the proper way to cancel out the error in the dataerror event.  I added the following line to it:

e.cancel = true

Is this the proper way to trap the error so it's not displayed?  What is the proper way to trap it if this isn't?  Thanks for your help.

Author

Commented:
On another note, the datagridview that I'm using is a custom one that I made to automatically mark certain columns and rows colors based on data in teh datagridview.  Is it possible that something in it is not configured properly?
John (Yiannis) ToutountzoglouInstructor Multiengine Pilot

Commented:
hi..For Datagridviews the data error is a proper way...you can also use a try  catch method to catch system argument exception.
if the e.cancel is fired i know that you are not able to leave the cell..
John (Yiannis) ToutountzoglouInstructor Multiengine Pilot

Commented:
can you please post some code ?

Author

Commented:
Hello,

    Below is the code for the datagridview control.  I did do some more testing on the datagridview and it is firing the cellChanged event.  The other wierd thing is that if I select 1 from the the list as the cells value and leave it does change the cell back to 0, but when I go back to it to change it again I have to select a value other than 1 to get the cellChanged event to fire again and some checking for the cell value within that event does show that the value is 1.  Thanks for your help.


This is the code for the datagidview:

Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.AllowUserToAddRows = False
        Me.AllowUserToDeleteRows = False
        Me.AutoGenerateColumns = False
        Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
        Me.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
        Me.RowHeadersVisible = False
        Me.MultiSelect = False

    End Sub

    Protected Overrides Sub OnDataSourceChanged(ByVal e As System.EventArgs)
        MyBase.OnDataSourceChanged(e)

        Dim dgColumn As DataGridViewColumn
        Dim dgRow As DataGridViewRow
        Dim color_scheme As String
        Dim bg_color As String
        Dim fg_color As String
        Dim sbgc_color As String
        Dim sfgc_color As String

        'Change row colors

        If (Me.Rows.Count > 0) Then

            For Each dgColumn In Me.Columns

                If (InStr(dgColumn.Name, "color") > 0) Then

                    For Each dgRow In Me.Rows

                        color_scheme = Me.Item(dgColumn.Index, dgRow.Index).Value.ToString

                        If (color_scheme <> "None") Then

                            bg_color = Mid(color_scheme, (InStr(color_scheme, "bc") + 3), (InStr(color_scheme, ";") - 4))
                            fg_color = Mid(color_scheme, (InStr(color_scheme, "fc") + 3), (InStr((InStr(color_scheme, "fc") + 3), color_scheme, ";") - (InStr(color_scheme, "fc") + 3)))
                            sbgc_color = Mid(color_scheme, (InStr(color_scheme, "sbgc") + 5), (InStr((InStr(color_scheme, "sbgc") + 5), color_scheme, ";") - (InStr(color_scheme, "sbgc") + 5)))
                            sfgc_color = Mid(color_scheme, (InStr(color_scheme, "sfgc") + 5), ((Len(color_scheme) + 1) - (InStr(color_scheme, "sfgc") + 5)))


                            dgRow.DefaultCellStyle.BackColor = ColorTranslator.FromHtml(bg_color)
                            dgRow.DefaultCellStyle.ForeColor = ColorTranslator.FromHtml(fg_color)
                            dgRow.DefaultCellStyle.SelectionBackColor = ColorTranslator.FromHtml(sbgc_color)
                            dgRow.DefaultCellStyle.SelectionForeColor = ColorTranslator.FromHtml(sfgc_color)

                        End If

                    Next

                End If

            Next

        End If

        'Change column colors for input columns
        For Each dgColumn In Me.Columns

            If (dgColumn.ReadOnly = False) Then

                dgColumn.DefaultCellStyle.BackColor = Color.LightGreen

            End If

        Next

    End Sub

Author

Commented:
It seems to only happen with columns that are tied to the datasource.  I have another grid in the program that has a column that is not tied to the datasource and it works good.  It's a checkbox column, but a checkbox column in another part of the program that is tied to the datasource doesn't work.  Could it be something with how I'm setting the datasource?

Author

Commented:
Ok.  I think I know what the issue is now.  The data I am retrieving is from a sql server stored procedure.  Is it possible that the datagrid realizing that the data would be readonly is turning the columns to be read only?  If it is, how can I make it stop doing that?

Thanks for the help.

Author

Commented:
Sorry, nevermind about it being a datasource problem.  The datasource is working find and the checkbox on the datagrid that referred to earlier is working fine now.  It had a problem with the datasource retrieval.  I have found that the datagrid is retaining the value that is selected and it's able to be accessed even after clicking off of the cell, but to the user it appears as 0.  This appears to be a problem completely around the comboboxcolumn itself.  Please help....
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
The datagridvewcomboboxcolumns item list was causing the problem.  Please refer to answer above.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.