Using the ADO.NET DataSet Designer
Article Index
Using the ADO.NET DataSet Designer
Binding and typing
Strong Typing

Binding

The table in the DataSet we have created can be bound to a control just as if it was associated with a database – this is because data binding has absolutely nothing to do with database!

To bind our DataSet place a DataGridView control on the form (WPF databinding works in a similar way). If you click on the control’s “Task List”, the small arrow icon in the top right-hand corner you can select a data source from within Project Data Sources and in this case select ImportantDates within DataSet1.

binding

This causes the DataGridView to display two columns and a dummy row corresponding to the definition of ImportantDates within DataSet1. You should also notice that a DataSet control called dataSet1and a BindingSource control have been automatically added to the form.

 

formbinding

 

The dataSet1 control is the instance that the DataGridView is bound to and to see some data appear we have to use this rather than the instance we created earlier:

DataSet1.ImportantDatesRow Row2 = 
     dataSet1.ImportantDates.
                   NewImportantDatesRow();
Row2.Event = "My Birthday";
Row2.Date = DateTime.Parse("1/2/2001");
dataSet1.ImportantDates.Rows.Add(Row2);

As soon as you add a row to dataSet1.ImportantDates it will be displayed in the DataGridView.

Similarly, and perhaps more surprisingly when you consider the work it entails, when the user adds or modifies data in the DataGridView this is automatically transferred to dataSet1.ImportantDates. It automatically conforms to the types defined in the table. So you might enter a string containing a date into the DataGridView but this is converted to a date when it’s stored in the table.

Advanced Columns

As well as creating tables with simple columns you can also add autonumbering and autocalculate columns.

For example, add, using the DataSet designer, a new column called Counter and set it to autoincrement in the Properties window. Next add a column called Double and enter into its Expression property Counter*2. If you now select the DataMember property of the ImportantDatesBindingSource control you will see the new columns appear and when you run the program the counter will increment every time you add a row and the count will automatically appear doubled in the Double column.

You can even define index keys and relationships between tables and these all work as if you had a database connected, even if there is none. You have to be careful however because not all of the methods will honour the relations, keys etc that you have defined.

In short the DataSet is a powerful data structure that you can use even if you don’t need a database and the DataSet Designer lets you create custom classes without having to write any code.

<ASIN:0672330539>

<ASIN:1430227036>



Last Updated ( Wednesday, 19 January 2011 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.