472,378 Members | 1,316 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,378 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 2991
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.