Formatting a DataGrid

  • Thread starter Thread starter Jeff Cook
  • Start date Start date
J

Jeff Cook

Hi

I have a DataGrid that I am using to display a .xml file (that has a schema
in a .xsd), like this:-

ds.ReadXmlSchema(sDataPath + "Data.xsd");
ds.ReadXml(sDataPath + "Data.xml", XmlReadMode.InferSchema);
grdXXXX.DataSource = ds;
FormatGridColumns(grdXXXX, ds.Tables["XXXX"]); // my routine below.

OK so far. Now I want to format the columns in the grid. I can't hard code
this as I don't know in advance what will be in the xml file.
I want to do check boxes if the column is boolean otherwise plain boxes,
except that I want to right justify decimal columns and left justify the
rest.

This what I have done so far (not much!):-

private void FormatGridColumns(DataGrid Grid, DataTable Table)
{
DataGridTableStyle ts = new DataGridTableStyle();
foreach (DataColumn c in Table.Columns)
{
ts.GridColumnStyles.Add(.............something
// test the datatype and set up the formatting ....
}
..........
}

Does that make sense? I need a pointer to get me going again!

Cheers

Jeff
 
Jeff,

In my opinion, this definitely makes sense. You'll also need to tune
MappingName-s for the table style and for the column styles, as well as keep
the right sequence of table style/column style creation (MSDN should have
something on this).
 
Dmitriy Lapshin said:
Jeff,

In my opinion, this definitely makes sense. You'll also need to tune
MappingName-s for the table style and for the column styles, as well as keep
the right sequence of table style/column style creation (MSDN should have
something on this).

Dmitriy

I'm sure that MSDN does ... I just can't find it! Guess it must be my
Delphi heritage - I'm not using the right keywords in searches!

Cheers

Jeff
--
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 said:
Hi

I have a DataGrid that I am using to display a .xml file (that has a schema
in a .xsd), like this:-

ds.ReadXmlSchema(sDataPath + "Data.xsd");
ds.ReadXml(sDataPath + "Data.xml", XmlReadMode.InferSchema);
grdXXXX.DataSource = ds;
FormatGridColumns(grdXXXX, ds.Tables["XXXX"]); // my routine below.

OK so far. Now I want to format the columns in the grid. I can't hard code
this as I don't know in advance what will be in the xml file.
I want to do check boxes if the column is boolean otherwise plain boxes,
except that I want to right justify decimal columns and left justify the
rest.

This what I have done so far (not much!):-

private void FormatGridColumns(DataGrid Grid, DataTable Table)
{
DataGridTableStyle ts = new DataGridTableStyle();
foreach (DataColumn c in Table.Columns)
{
ts.GridColumnStyles.Add(.............something
// test the datatype and set up the formatting ....
}
..........
}

Does that make sense? I need a pointer to get me going again!

Cheers

Jeff
 
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);
}
 
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

Dmitriy Lapshin said:
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 ----------------------

I have looked at what you have quoted above and at my code below and I must
be missing the point. As far as I can see, I am creating a
DataGridTableStyle, then adding GridColumnStyles to that and finally adding
the DataGridTableStyle to the DataGrid.TableStyle. I have tried
DataGrid.TableStyle.Clear() before adding as well. Maybe I misunderstand
the quoted stuff? I can't be doing it VERY wrong as the code does make the
boolean fields into check boxes and the decimal fields do display as
currency - it is just this alignment that is wrong.
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.

I thought "YES", that'll be it. Changed and it didn't make any difference
:-(

Thanks for your help

Cheers

Jeff
 
Back
Top