14.11 Adding New Tables to a DataSet

 <  Day Day Up  >  

14.11 Adding New Tables to a DataSet

You want to dynamically add a new table to an existing DataSet .


Technique

A DataSet contains a property named Tables , which is a dictionary-based collection containing DataTable objects. To add a new DataTable , call the Add method from the Tables collection, passing a string denoting the name of a table, an existing DataTable object, or no parameters if you want the name to be generated for you automatically. The name that is generated uses the syntax Table N where N denotes the next number in a sequence for multiple tables. The following example creates a new table within a DataSet and binds a DataGrid on a Windows Form to that DataSet :

 
 public Form1() {     InitializeComponent();     ds = new DataSet();     ds.Tables.Add("ParentTable");     dataGrid1.SetDataBinding( ds, "ParentTable" ); } 

Each table contains a collection of DataColumn objects accessed through its Columns property. A DataColumn contains at least a string denoting its name and the data type for values within the column. You can optionally specify an Expression to create a data column that calculates a value using the values in other columns. The following example creates a DataTable that is used like a spreadsheet to calculate the y coordinate of a line using the equation y=mx+b . Within the code, you can also see how to create a column that increments the previous row's value and forces a user to keep the number unique. You do so by setting the AutoIncrement and Unique properties of a DataColumn object to true :

 
 public Form1() {     InitializeComponent();     ds = new DataSet();     DataTable table = ds.Tables.Add("MyTable");     DataColumn keyCol = new DataColumn();     keyCol.ColumnName = "ID";     keyCol.DataType = typeof( int );     keyCol.AutoIncrement = true;     keyCol.Unique = true;     table.Columns.Add( keyCol );     table.Columns.Add( "M", typeof(int) ).DefaultValue = 0;     table.Columns.Add( "X", typeof(int) ).DefaultValue = 0;     table.Columns.Add( "B", typeof(int) ).DefaultValue = 0;     table.Columns.Add( "Y", typeof(int), "M*X+B" ).DefaultValue = 0;     dataGrid1.SetDataBinding( ds, "MyTable" ); } 

Comments

Earlier in this chapter, it was mentioned that a DataGrid behaves similarly to that of a spreadsheet application. Until this point, the similarity was the visual appearance of the DataGrid . In this section, you get a first glimpse of how to use a DataTable and its associated DataColumn s to mimic the functionality of a spreadsheet. Although the expression support available for the DataColumn falls quite short of the functionality of an Excel column, it does allow you to perform several common calculations, including standard deviations, variance, and substring determination.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net