?
Solved

vb.net 2010 DataGridViewComboCox column problems

Posted on 2011-04-29
16
Medium Priority
?
658 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.
0
Comment
Question by:rcblevins
  • 10
  • 4
  • 2
16 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35491962
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..
0
 

Author Comment

by:rcblevins
ID: 35492038
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.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35492137
If I remember correctly, you said in previous question the grid is readonly right?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:rcblevins
ID: 35492293
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?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35493893
No that should not make a difference.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35494436
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
0
 

Author Comment

by:rcblevins
ID: 35495696
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.
0
 

Author Comment

by:rcblevins
ID: 35495833
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?
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35496266
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..
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35496347
can you please post some code ?
0
 

Author Comment

by:rcblevins
ID: 35498147
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
0
 

Author Comment

by:rcblevins
ID: 35505005
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?
0
 

Author Comment

by:rcblevins
ID: 35505075
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.
0
 

Author Comment

by:rcblevins
ID: 35506751
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....
0
 

Accepted Solution

by:
rcblevins earned 0 total points
ID: 35507541
Hello again.  I got the problem resolved apparently (at least in the case of using integers) you cant define those in the datagridviewcomboboxcolumns item collection list.  This was causing the subsequent system.argumentexception error and was also causing the problem with the value being reset to it's original state, which I assume had something to do with the the validation that the datagridview performs.  Anyway creating a bindinglist (of integer) and adding the values to it and the having the combobox column's datasource point to it fixed the problems all around.  Below is where If found how to set up the binding list:

http://www.mugaland.com/Post.aspx?PostID=22

0
 

Author Closing Comment

by:rcblevins
ID: 35711398
The datagridvewcomboboxcolumns item list was causing the problem.  Please refer to answer above.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

862 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