Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGridView Cell Formatting

Status
Not open for further replies.

abcfantasy

Programmer
Jun 3, 2006
110
DK
Hello, I've got the following case:

I have a DataGridView which is dynamically (during runtime) bounded to a DataTable. What I wish is to format certain columns, but it seems all formatting is being ignored.

I have tried:
Code:
dataGridView.Columns["ColumnName"].DefaultCellStyle.Format = "c"
but no result.

Then I tried using the CellFormatting event:
Code:
private void dataGridView_CellFormatting( object sender, DataGridViewCellFormattingEventArgs e )
        {
            if ( e.ColumnIndex = 1 )
                e.CellStyle.Format = "c"
        }
But still no use.

Is it because of the data source being a DataTable? Perhaps formatting is in someway overridden?

Any help is appreciated.
Thanks.




|| ABC
 
Is your method being hit? (Check in the debugger)

(it should be, as the cell_Formatting event is fired each and every time the grid is painted. (So its best not to do anythign too intensive here if you can avoid it.), but lets start at first principles.

Secondly is e.CellStyle.Format = "c" valid for the data you are putting in that cell?
You are checking for a ColumnIndex, then trying to format potentially empty data.

K

 
The event is being fired, yes.

The above format was only an example. I tried various that should be valid for (in this case) a UInt32.

I tried "x" for hex for instance, or "F3" for fixed-point but to no use. The data is always filled, never empty (0 or greater).

Is it possible that since I'm adding object arrays to the DataTable, it does not recognize the types of each data? I have the various attributes of a class (with specific types), they are put into an array of type object and added to the DataTable. That can be the cause?

Thanks again.

|| ABC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top