开发者

Format the list items from the DataGridViewComboBox

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开发者_JS百科 can clearly see that it doesn't look as great as it could.

Format the list items from the DataGridViewComboBox

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

Thanks.


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"
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜