Mapping DataGridTableStyle to DataGrid

S

Scott

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.xsd");
ds.ReadXml(@"C:\Doc....ShoppingList.xml",System.Data.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.
 
B

Bart Mermuys

Hi,

Scott said:
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.xsd");
ds.ReadXml(@"C:\Doc....ShoppingList.xml",System.Data.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.
 
S

scott

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 said:
Hi,

Scott said:
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.xsd");
ds.ReadXml(@"C:\Doc....ShoppingList.xml",System.Data.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top