473,785 Members | 2,720 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mapping DataGridTableSt yle 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 DataGridTableSt yle 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.TableS tyles.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.Dat aSet ds = new System.Data.Dat aSet();

ds.ReadXmlSchem a(@"C:\Doc....S hoppingListData Set.xsd");
ds.ReadXml(@"C: \Doc....Shoppin gList.xml",Syst em.Data.XmlRead Mode.Auto);
System.Data.Dat aTable groupsDT = ds.Tables[0];
System.Data.Dat aTable itemsDT = ds.Tables[1];

System.Windows. Forms.DataGridT ableStyle ts = new DataGridTableSt yle();
//dataGrid1.DataM ember = "shopping";
ts.MappingName = itemsDT.TableNa me;

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

dataGrid.DataSo urce = itemsDT;
dataGrid.TableS tyles.Add(ts);
dataGrid.TableS tyles[0].GridColumnStyl es.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 DataGridTableSt yle added to
the datagrid all works fine.
Nov 17 '05 #1
2 3121
Hi,

"Scott" <sc***********@ hotamil.com> wrote in message
news:dk******** **@newsg4.svr.p ol.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 DataGridTableSt yle 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.TableS tyles.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.BindingConte xt = this.BindingCon text;
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.Dat aSet ds = new System.Data.Dat aSet();

ds.ReadXmlSchem a(@"C:\Doc....S hoppingListData Set.xsd");
ds.ReadXml(@"C: \Doc....Shoppin gList.xml",Syst em.Data.XmlRead Mode.Auto);
System.Data.Dat aTable groupsDT = ds.Tables[0];
System.Data.Dat aTable itemsDT = ds.Tables[1];

System.Windows. Forms.DataGridT ableStyle ts = new DataGridTableSt yle();
//dataGrid1.DataM ember = "shopping";
ts.MappingName = itemsDT.TableNa me;

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

dataGrid.DataSo urce = itemsDT;
dataGrid.TableS tyles.Add(ts);
dataGrid.TableS tyles[0].GridColumnStyl es.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 DataGridTableSt yle 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******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

"Scott" <sc***********@ hotamil.com> wrote in message
news:dk******** **@newsg4.svr.p ol.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 DataGridTableSt yle 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.TableS tyles.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.BindingConte xt = this.BindingCon text;
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.Dat aSet ds = new System.Data.Dat aSet();

ds.ReadXmlSchem a(@"C:\Doc....S hoppingListData Set.xsd");
ds.ReadXml(@"C: \Doc....Shoppin gList.xml",Syst em.Data.XmlRead Mode.Auto);

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

System.Windows. Forms.DataGridT ableStyle ts = new DataGridTableSt yle();
//dataGrid1.DataM ember = "shopping";
ts.MappingName = itemsDT.TableNa me;

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

dataGrid.DataSo urce = itemsDT;
dataGrid.TableS tyles.Add(ts);
dataGrid.TableS tyles[0].GridColumnStyl es.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 DataGridTableSt yle 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
3958
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 to be added. I was told to use a DataView and set AddNew to false (thanks for the info btw) but now I can't use the DataGridTableStyle to hide the columns I don't want. Is there any way to have both hidden columns and disable AddNew in a...
2
1761
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 'DataGridTableStyle 'could not be found" also I have the following at the begining of the program.. using System;
2
2103
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 both associated with the ODD-numbered rows of the grid as stated in msdn?? Thank you and happy new year!!
2
1232
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 name the name of the Columns in the database or is it something else? /Lars
2
2424
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 maybe the problem was I had more columns being put into the DataGrid than I was formatting. I then created a new table and adding only the five mapped columns to it. That didn't help either. I'm hiding column 3 because I need its value but...
3
4295
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 great. The problem is the columns are in the wrong order. OK. I'll use the datagridtablestyle. But when I add the following statement; Dim tableStyle As New DataGridTableStyle I get an error when I build the project;
7
12631
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 the background for my question... Before I switched my application over to the Fx 2.0, I used a DataGrid to display my data. I would store different DataGridTableStyles (each one with a custom set of columns) in the DataGrid.TableStyles property...
0
1654
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 not work at all. this is my code : first, the alarm class. class Alarm
0
1219
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 datagrid using a datagridtablestyle. It works for the first table, but if a user selects another table, it errors out. As a test I have the two functions CreateDataTable which creates a table with data and SetupTableStyles which sets up the...
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.