473,395 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Mapping DataGridTableStyle to DataGrid

Hi all.

A few days ago i ask this question and got a good quick response. I tried
out what they said and it worked. However I have now come to try the same
thing in another program and it does not seem to be working.

I have a dataset that gets its info from an xmlfile and an xml schema. I
then use a DataTable to link to the dataset. I then create a DataGrid and
link it to the Datatable.

So far everything works. Its when i come to create a DataGridTableStyle it
all starts to go wrong. i give the Tablestyle the mapping name of the
Datatable's TableName. Then i add it to the datagrid using the
dataGrid.TableStyles.Add() method. Now when i come to use the tablestyle in
the datagrid to remove a column i get an error saying that there is no
columns.

The code below is what i am trying to do. Can any one see anything wrong
with it ?

Many thanks for taking a look at this.

Scott.

System.Data.DataSet ds = new System.Data.DataSet();

ds.ReadXmlSchema(@"C:\Doc....ShoppingListDataSet.x sd");
ds.ReadXml(@"C:\Doc....ShoppingList.xml",System.Da ta.XmlReadMode.Auto);
System.Data.DataTable groupsDT = ds.Tables[0];
System.Data.DataTable itemsDT = ds.Tables[1];

System.Windows.Forms.DataGridTableStyle ts = new DataGridTableStyle();
//dataGrid1.DataMember = "shopping";
ts.MappingName = itemsDT.TableName;

System.Windows.Forms.DataGrid dataGrid = new DataGrid();

dataGrid.DataSource = itemsDT;
dataGrid.TableStyles.Add(ts);
dataGrid.TableStyles[0].GridColumnStyles.RemoveAt(0); // <--- Index is
out of range. Must be non-negative and less than the sieze of the
collectoin.

if i atempt to view the data drig with out the DataGridTableStyle added to
the datagrid all works fine.
Nov 17 '05 #1
2 3093
Hi,

"Scott" <sc***********@hotamil.com> wrote in message
news:dk**********@newsg4.svr.pol.co.uk...
Hi all.

A few days ago i ask this question and got a good quick response. I tried
out what they said and it worked. However I have now come to try the same
thing in another program and it does not seem to be working.

I have a dataset that gets its info from an xmlfile and an xml schema. I
then use a DataTable to link to the dataset. I then create a DataGrid and
link it to the Datatable.

So far everything works. Its when i come to create a DataGridTableStyle it
all starts to go wrong. i give the Tablestyle the mapping name of the
Datatable's TableName. Then i add it to the datagrid using the
dataGrid.TableStyles.Add() method. Now when i come to use the tablestyle
in
the datagrid to remove a column i get an error saying that there is no
columns.
The problem is your orphaned DataGrid, when you add an empty DGTableStyle
then the DataGrid can auto-generate the DGColumnStyle's depending on the
DataSource. It requires databinding to work to get the columns from the
DataSource, but databinding doesn't work when the Control isn't visible or
not on the Form...

A few things you can do (choose one):

- You can add the DataGrid to the Controls collection before you add the
DGTableStyle to the DataGrid. The Visible property must be true before you
add it to the Controls collection but can be set to false afterwards:
Controls.Add( dg );

- Instead of deleting auto-generated DGColumnStyle you could create and add
the ones you want to the DGTableStyle yourself. If the DGTableStyle isn't
empty when you add it to the DataGrid then it won't auto-generate any
columns.

- The reason DataBinding doesn't work is because it needs a BindingContext
and by default Controls use their parent's one, up to the Form. But your
DataGrid has no parent yet so it doesn't have a BindingContext. You can
however set it explicitly before adding the DGTableStyle:
dg.BindingContext = this.BindingContext;
HTH,
Greetings

The code below is what i am trying to do. Can any one see anything wrong
with it ?

Many thanks for taking a look at this.

Scott.

System.Data.DataSet ds = new System.Data.DataSet();

ds.ReadXmlSchema(@"C:\Doc....ShoppingListDataSet.x sd");
ds.ReadXml(@"C:\Doc....ShoppingList.xml",System.Da ta.XmlReadMode.Auto);
System.Data.DataTable groupsDT = ds.Tables[0];
System.Data.DataTable itemsDT = ds.Tables[1];

System.Windows.Forms.DataGridTableStyle ts = new DataGridTableStyle();
//dataGrid1.DataMember = "shopping";
ts.MappingName = itemsDT.TableName;

System.Windows.Forms.DataGrid dataGrid = new DataGrid();

dataGrid.DataSource = itemsDT;
dataGrid.TableStyles.Add(ts);
dataGrid.TableStyles[0].GridColumnStyles.RemoveAt(0); // <--- Index is
out of range. Must be non-negative and less than the sieze of the
collectoin.

if i atempt to view the data drig with out the DataGridTableStyle added to
the datagrid all works fine.

Nov 17 '05 #2
Thank you very much for the help. I have been trying all day to get this to
work and as soon as i did what you said it worked fine.

Many thanks

Scott.

"Bart Mermuys" <bm*************@hotmail.com> wrote in message
news:uz**************@TK2MSFTNGP09.phx.gbl...
Hi,

"Scott" <sc***********@hotamil.com> wrote in message
news:dk**********@newsg4.svr.pol.co.uk...
Hi all.

A few days ago i ask this question and got a good quick response. I tried out what they said and it worked. However I have now come to try the same thing in another program and it does not seem to be working.

I have a dataset that gets its info from an xmlfile and an xml schema. I then use a DataTable to link to the dataset. I then create a DataGrid and link it to the Datatable.

So far everything works. Its when i come to create a DataGridTableStyle it all starts to go wrong. i give the Tablestyle the mapping name of the
Datatable's TableName. Then i add it to the datagrid using the
dataGrid.TableStyles.Add() method. Now when i come to use the tablestyle in
the datagrid to remove a column i get an error saying that there is no
columns.
The problem is your orphaned DataGrid, when you add an empty DGTableStyle
then the DataGrid can auto-generate the DGColumnStyle's depending on the
DataSource. It requires databinding to work to get the columns from the
DataSource, but databinding doesn't work when the Control isn't visible or
not on the Form...

A few things you can do (choose one):

- You can add the DataGrid to the Controls collection before you add the
DGTableStyle to the DataGrid. The Visible property must be true before

you add it to the Controls collection but can be set to false afterwards:
Controls.Add( dg );

- Instead of deleting auto-generated DGColumnStyle you could create and add the ones you want to the DGTableStyle yourself. If the DGTableStyle isn't
empty when you add it to the DataGrid then it won't auto-generate any
columns.

- The reason DataBinding doesn't work is because it needs a BindingContext
and by default Controls use their parent's one, up to the Form. But your
DataGrid has no parent yet so it doesn't have a BindingContext. You can
however set it explicitly before adding the DGTableStyle:
dg.BindingContext = this.BindingContext;
HTH,
Greetings

The code below is what i am trying to do. Can any one see anything wrong with it ?

Many thanks for taking a look at this.

Scott.

System.Data.DataSet ds = new System.Data.DataSet();

ds.ReadXmlSchema(@"C:\Doc....ShoppingListDataSet.x sd");
ds.ReadXml(@"C:\Doc....ShoppingList.xml",System.Da ta.XmlReadMode.Auto);

System.Data.DataTable groupsDT = ds.Tables[0];
System.Data.DataTable itemsDT = ds.Tables[1];

System.Windows.Forms.DataGridTableStyle ts = new DataGridTableStyle();
//dataGrid1.DataMember = "shopping";
ts.MappingName = itemsDT.TableName;

System.Windows.Forms.DataGrid dataGrid = new DataGrid();

dataGrid.DataSource = itemsDT;
dataGrid.TableStyles.Add(ts);
dataGrid.TableStyles[0].GridColumnStyles.RemoveAt(0); // <--- Index is
out of range. Must be non-negative and less than the sieze of the
collectoin.

if i atempt to view the data drig with out the DataGridTableStyle added to the datagrid all works fine.


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rxd | last post by:
I have a Datagrid that should have a couple of hidden columns. I used a DataGridTableStyle to hide the columns and it was working fine except I needed to prevent the DataGrid from allowing new rows...
2
by: Carlos | last post by:
Does DataGridTableStyle work in ASP.net ? any time I'm tring to define a varibale lke DataGridTableStyle MyStyle = new DataGridTableStyle(); it give me "The Type or name sapce...
2
by: kerpal | last post by:
Hi all, 1. Could anyone pls clarify the difference between setting the AlternatingBackColor property in DataGridTableStyle and in DataGrid?? 2. How come BackColor and AlternatingBackColor are...
2
by: Lars Netzel | last post by:
I'm reading the help in VS 2003 about DataGridTableStyle and there's something called Mappingname but I can't figure out what that is For example if I bind a datagrid from db data is the mapping...
2
by: Brett Romero | last post by:
I can't find what exactly I'm doing wrong that the following DataGridTableStyle is not working on my table. The result set returns 21 columns. I'm only formatting 5 via the TableStyle. I thought...
3
by: nita | last post by:
I'm just starting out, and it's incredibly frustrating when I see sample code and then try to implement it. Case in point. I'm populating a collection then binding it to a datagrid. That works...
7
by: Mitchell S. Honnert | last post by:
Is there an equivalent of the DataGrid's DataGridTableStyle for the DataGridView? If not, is there an easy way to duplicate the DataGridTableStyle's functionality for the DataGridView? Here's...
0
by: Guillaume | last post by:
Hi, I have an ArrayList filled with many Alarm object ( i will ad next this class definition). I use this arraylist as a datasource for my datagrid. I want to apply some table style, but it does...
0
by: Tommy152 | last post by:
I have a drop down box with a list of tables from a SQL CE database running on Windows Mobile 5, and once a user selects a table, all the data is bound to a datagrid. Now I'm trying to format the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.