Click here to Skip to main content
14,827,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have following datagrid

<DataGrid x:Name="dataGrid" ScrollViewer.CanContentScroll="True" SelectionUnit="Cell" 
                    Height="Auto" Margin="5,58,5,50" ScrollViewer.VerticalScrollBarVisibility="Visible" 
                    AutoGenerateColumns="True" AlternatingRowremoved="#FF9BEEE2" IsReadOnly="True" AutoGeneratingColumn="dataGrid_AutoGeneratingColumn" SelectionMode="Single">

Some of the columns contain floating number and I would like to format the number with fixed number of decimals.

Second question:

I would also like to change the background color of a cell based on the value.
Updated 17-Jan-15 21:51pm

1 solution

Hi Tommy,

As far as I know, you can't do this with autogenerated columns via xaml.
You have several options to do it:

  1. Iterate through the columns after the binding and set either the CellStyle or the CellTemplate.
  2. Create the columns in advance and then override the CellStyle or CellTemplate.
  3. Replace the data binding of each column with a new binding with a string format.

If I'll come up with another option I'll comment again.

Good luck,
Tommy Jensen 24-Jan-15 4:24am
Hmm I was able to change the collumn headers during generation of the grid with the AutoGeneratedCollumn method so would have hoped it would possible to change alignment for example also. At minimum I would like to change cell alignment to right on some of the collumns.
InbarBarkai 25-Jan-15 0:27am
Try to assign a style to the column's cell style property

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900