Maybe, I think, things are too simple. I have a datagrid, dynamically adding columns from code behind. This works. Then I have columns with integer-values, which I would like to see right-aligned in the column, but it fails. My Code is:
public List<List<int>> IntegerData { get; set; }................
DataGridTextColumn textC = new DataGridTextColumn();
textC.Header = ("Column");
textC.Binding = new Binding(string.Format("[{0}]", i));
textC.Binding.StringFormat = "00000";
and at the end I have something like:
myDataGrid.Itemssource = IntegerData
With the textC.Binding.String.Format the problem is: (First: it seems, that the stringFormat-LIne correctly overloads the previous line)
Format "0" gives an integer "1" "2", which is correct.
Format "00000" gives "00001" "00002" "00003" which is correct for String-Format, but not, what I would like to have
BUT:
Format "######" gives also "1" "2" .. and so on (left-aligned, no leading blanks)
Due to formatting-rules I would have expected (or hoped), that ##### is placeholder as described and the result is "bbbb1" , "bbbb2" right-aligned. (b is here for blank!)
So Binding seems to deal correctly with the "00000" Format, but not with "######" Format. Has anybody any idea or experience with that idea? (all other Bindings, I find in internet, are much more complicated)
You can solve your problem by creating a style rather than formating the text in the cell.
first, create a style:
Style style = new Style(typeof(DataGridCell));
style.Setters.Add(new Setter(HorizontalAlignmentProperty, HorizontalAlignment.Right));
than assign the style to the column
textC.CellStyle = style;