473,498 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5543
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2287
by: Orhan Toker | last post by:
Hi, Is it possible to format a datagrid cell containing string type data. (For example, i want "2122131199" value to be displayed as (212)-213-11-99. Also text boxs hasn't that kind of property...
0
1486
by: Josh Harris | last post by:
Here is my issue: I have a datagrid that is populated with a datatable. I want the columns of the datagrid to be sortable. I also want to format the numeric columns such as two decimal places...
0
1809
by: Frnak McKenney | last post by:
One of the reasons given for the Allied victory in WWI is that the Nazi armament industry invested so much effort in creating new weapons (e.g. the jet plane) it wasn't able to develop any of them...
7
4662
by: Matthew Wieder | last post by:
Hi - I have a datagrid that has a black header and the rows alternate white and gray. The problem is that until items are added to the grid, the grid appears as a large black rectangle, which is...
3
4056
by: McNutt Consulting | last post by:
I'm trying to implement a datagrid with dynamically created columns. The data is coming out of a very simple XML file, and it's bound through a dataview. The datagrid is just a shell definition...
1
1288
by: Ed | last post by:
Hi, I would like to build a datagrid with many columns, but one of them should be a big column with a "Note" field, so I want to write a row with many columns and the next row should have just...
5
2728
by: tshad | last post by:
I have been trying to figure out what the Datagrid is doing to create its formatting. I found that some of my Datagrids have a 3D type of border and sometime it has a straight line. I finally...
5
4551
by: cwbp17 | last post by:
Hi all, Have a datagrid that displays the price column of a table. Went to the Datagrid's Property builder and set the 'Data Formatting expression' for the PRICE column to {0:c}, so that the...
4
3215
by: hope | last post by:
Hi, How can I format a string field using Data Formatting Expression property in datagrid? For example: format last name from BROWN to Brown. Thanks
2
1608
by: John Walker | last post by:
Hello, Below is my code for exporting a datagrid to Excel. It works fine, but we're hoping to format the output as well - setting the font size and type, and giving each column a specific width,...
0
7004
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7167
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4915
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4593
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.