471,075 Members | 948 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 software developers and data experts.

Formatting a DataGrid

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
Nov 15 '05 #1
5 5364
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 [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:ed**************@TK2MSFTNGP11.phx.gbl...
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


Nov 15 '05 #2

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:u9**************@TK2MSFTNGP09.phx.gbl...
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" <jeffcATaspectDOTcoDOTnz> wrote in message
news:ed**************@TK2MSFTNGP11.phx.gbl...
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

Nov 15 '05 #3
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);
}
Nov 15 '05 #4
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);
}


Nov 15 '05 #5
Dmitriy

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:%2******************@TK2MSFTNGP09.phx.gbl...
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
"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);
}

Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Orhan Toker | last post: by
reply views Thread by Josh Harris | last post: by
7 posts views Thread by Matthew Wieder | last post: by
3 posts views Thread by McNutt Consulting | last post: by
1 post views Thread by Ed | last post: by
5 posts views Thread by tshad | last post: by
4 posts views Thread by hope | last post: by
2 posts views Thread by John Walker | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.