Hi Jeff,
First, about the sequence. The "DataGridTableStyle Class" MSDN topic gives
this order in the code example
a) Populate GridColumnStyles collections for each TableStyle
b) Add created TableStyles to the TableStyles collection of the data grid.
And the "DataGridColumnStyle Class" topic adds to that:
--------------------- Quote begins ---------------------
CAUTION Always create DataGridColumnStyle objects and add them to the
GridColumnStylesCollection before adding DataGridTableStyle objects to
the GridTableStylesCollection. When you add an empty DataGridTableStyle
to the collection, DataGridColumnStyle objects are automatically generated
for you. Consequently, an exception will be thrown if you try to add new
DataGridColumnStyle objects with duplicate MappingName values to the
GridColumnStylesCollection.
---------------------- Quote ends ----------------------
As for the decimal formatting, I really don't know what to say. Try setting
the format first and alignment second, probably - it might be that setting
the Format property somehow affects the alignment.
--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE
"Jeff Cook" <jeffcATaspectDOTcoDOTnz> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi
I have made progress on displaying my XML data in a DataGrid. I have
worked out how to set the column formats depending on the contents of the XML
file and its schema.
BUT ... I have a mystery. Why doesn't the column alignment work for
deciaml fields - I try to make them right align, but it they come out left
aligned like all the other fields.
Code pasted below.
Cheers
Jeff
================================
private void FormatGridColumns(DataGrid Grid, DataTable Table)
{
DataGridTableStyle ts = new DataGridTableStyle();
ts.MappingName = Table.Namespace;
foreach (DataColumn c in Table.Columns)
{
if (c.DataType == System.Type.GetType("System.Boolean"))
{
DataGridBoolColumn bc = new DataGridBoolColumn();
bc.MappingName = c.ColumnName;
ts.GridColumnStyles.Add(bc);
}
else
{
DataGridTextBoxColumn tc = new DataGridTextBoxColumn();
tc.MappingName = c.ColumnName;
if (c.DataType == System.Type.GetType("System.Decimal"))
{
tc.Alignment = HorizontalAlignment.Right; // why doesn't this
work?
tc.Format = "c"; // but this DOES work!
}
ts.GridColumnStyles.Add(tc);
}
}
Grid.TableStyles.Add(ts);
}