By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,002 Members | 1,449 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,002 IT Pros & Developers. It's quick & easy.

Mapping DataGridTableStyle to DataGrid

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.