views:

25

answers:

1

Hello,

I'm displaying prices on a DataGridView (on a WinForms application developped in C# on VS 2008). The column type is DataGridViewComboBoxColumn.

The DefaultCellStyle.Format property for this column is set to "C2" and the prices are indeed formatted as currency in the cells of this column.

However, when the user clicks the ComboBox to selected a value, the value on the list are not formatted. For example, instead of seeing:

3.90 €
4.50 €
5.95 €

They see

3.9
4.5
5.95

The values is also left-aligned while I would prefer to have them right-aligned.

Here is a picture showing the current behaviour and we can clearly see that it doesn't look as great as it could.

alt text

Is it possible to get the result I'm after?

Thanks.

+1  A: 

Just handle the EditingControlShowing event on your DataGridView and do:

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    ComboBox cb = e.Control as ComboBox;
    if (cb != null)
        cb.FormatString = "<your format string>"; // e.g. "C2"
}
digEmAll
Great, it is working thanks.How do I do if I have several ComboBox columns that I want to format differently? In most events, I have a e.ColumnIndex property which allows me to see for which column the event has been raised for, but it doesn't seem to be the case for the EditingControlShowing event? Thanks!
Kharlos Dominguez
Well, I just did this : "if (gridView.CurrentCell.ColumnIndex == gridView.Columns["Price"].Index)"and it does the trick, but if there is a better approach, please do not hesitate to let me know. Thanks.
Kharlos Dominguez
Yes, I think that's the right method ;)
digEmAll