rcblevins
asked on
vb.net 2010 DataGridViewComboCox column problems
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.
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.
ASKER
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.
If I remember correctly, you said in previous question the grid is readonly right?
ASKER
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?
No that should not make a difference.
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
a very simple example ...(that works properly) is here
http://vb.net-informations.com/datagridview/vb.net_datagridview_combobox.htm
ASKER
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.
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.
ASKER
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?
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..
if the e.cancel is fired i know that you are not able to leave the cell..
can you please post some code ?
ASKER
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 = DataGridViewAutoSizeColumn sMode.None
Me.AutoSizeRowsMode = DataGridViewAutoSizeRowsMo de.AllCell s
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.ToStrin g
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.Bac kColor = ColorTranslator.FromHtml(b g_color)
dgRow.DefaultCellStyle.For eColor = ColorTranslator.FromHtml(f g_color)
dgRow.DefaultCellStyle.Sel ectionBack Color = ColorTranslator.FromHtml(s bgc_color)
dgRow.DefaultCellStyle.Sel ectionFore Color = ColorTranslator.FromHtml(s fgc_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
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 = DataGridViewAutoSizeColumn
Me.AutoSizeRowsMode = DataGridViewAutoSizeRowsMo
Me.RowHeadersVisible = False
Me.MultiSelect = False
End Sub
Protected Overrides Sub OnDataSourceChanged(ByVal e As System.EventArgs)
MyBase.OnDataSourceChanged
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.ToStrin
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
sbgc_color = Mid(color_scheme, (InStr(color_scheme, "sbgc") + 5), (InStr((InStr(color_scheme
sfgc_color = Mid(color_scheme, (InStr(color_scheme, "sfgc") + 5), ((Len(color_scheme) + 1) - (InStr(color_scheme, "sfgc") + 5)))
dgRow.DefaultCellStyle.Bac
dgRow.DefaultCellStyle.For
dgRow.DefaultCellStyle.Sel
dgRow.DefaultCellStyle.Sel
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.
End If
Next
End Sub
ASKER
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?
ASKER
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.
Thanks for the help.
ASKER
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....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The datagridvewcomboboxcolumns item list was causing the problem. Please refer to answer above.
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..