Resolved How to format drop down items in a datagridview combobox column

theskiguy

New member
Joined
Mar 18, 2013
Messages
2
Programming Experience
3-5
Hi Experts:

I have a bound datagridview with a combobox column that is unbound. The combobox column is unbound as this will only contain a list of percentages on 10% increments. The values in my database are stored in decimal format so like .80 for 80%. I also added the unbound values for the combobox the same way.

I have the datagridview column format set to "P0" to show percentage so when I refresh the grid, it shows 0.8 as 80 % exactly like I want. The issue I have is when I select the dropdown to pick a different percentage the list of available options that appear still show the decimal values and not the %'s. After selecting the value, it changes it to a percentage but I was wondering if there is anyway to get the dropdown items format to show %?

Thanks,
 
Solution
The dropdown is the editing control, which is a separate combobox control. Handle the grids EditingControlShowing event, where you check that you have the correct column, cast e.Control to type ComboBox and set its FormatString to "P0" as well.

Next problem is that the editing control will now return the formatted string value, so you must then handle the grids CellParsing event, where you parse the percent string from e.Value as number and divide by 100, then set e.Value and e.ParsingApplied=True.
The dropdown is the editing control, which is a separate combobox control. Handle the grids EditingControlShowing event, where you check that you have the correct column, cast e.Control to type ComboBox and set its FormatString to "P0" as well.

Next problem is that the editing control will now return the formatted string value, so you must then handle the grids CellParsing event, where you parse the percent string from e.Value as number and divide by 100, then set e.Value and e.ParsingApplied=True.
 
Solution
The dropdown is the editing control, which is a separate combobox control. Handle the grids EditingControlShowing event, where you check that you have the correct column, cast e.Control to type ComboBox and set its FormatString to "P0" as well.

Next problem is that the editing control will now return the formatted string value, so you must then handle the grids CellParsing event, where you parse the percent string from e.Value as number and divide by 100, then set e.Value and e.ParsingApplied=True.

Got sidetracked on other projects so just got back to this today. I have never used these 2 events before but with your good explanation, I was able to it to work with the following code. Thanks for the help!

Private Sub DGVSchedule_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DGVSchedule.EditingControlShowing

'Look for the ComboBox column
If Me.DGVSchedule.Columns(Me.DGVSchedule.CurrentCell.ColumnIndex).Name.ToUpper = "EFFICIENCY" Then
Dim CB As ComboBox = TryCast(e.Control, ComboBox)
CB.FormatString = "P0"
End If
End Sub

Private Sub DGVSchedule_CellParsing(sender As Object, e As DataGridViewCellParsingEventArgs) Handles DGVSchedule.CellParsing

If Me.DGVSchedule.Columns(e.ColumnIndex).Name.ToUpper = "EFFICIENCY" Then
Try
e.Value = CDec(e.Value.ToString.Replace(" %", "") / 100)
e.ParsingApplied = True
Catch ex As Exception
e.ParsingApplied = False
End Try
End If
End Sub
 
Back
Top