472,965 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,965 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 5515
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
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
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
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
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
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
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
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
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
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
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
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.