After creating the tables that are part of an application, before performing any operation on a table, you must first retrieve its reference. This can be done by locating the particular desired table from the collection. To locate a table in the DataSet.Tables collection, the DataTableCollection class is equipped with the Item indexed property in three versions. To locate a table using its name, use the following version of this property: public DataTable this[string name] {get;} To use this property, enter the object name of the table in the square brackets of the DataTableCollection[] property. Here is an example: namespace VideoCollection1 { public partial class Exercise : Form { DataTable tblActors; DataTable tblFormats; DataTable tblRatings; DataTable tblDirectors; DataTable tblVideoCategories; DataSet dsVideoCollection; public Exercise() { InitializeComponent(); } private void btnDocument_Click(object sender, EventArgs e) { dsVideoCollection = new DataSet("Videos"); tblDirectors = new DataTable(); tblDirectors.TableName = "Directors"; dsVideoCollection.Tables.Add(tblDirectors); tblVideoCategories = new DataTable("Categories"); dsVideoCollection.Tables.Add(tblVideoCategories); tblRatings = dsVideoCollection.Tables.Add(); tblActors = dsVideoCollection.Tables.Add("Actors"); tblFormats = dsVideoCollection.Tables.Add("Formats"); DataTable tbl = dsVideoCollection.Tables["Directors"]; txtVideo.Text = tbl.TableName; } } } Instead of locating a table by its name, you can use its index from the collection. To do this, you can use the second version of the DataTableCollection[] property. Its syntax is: public DataTable this[int index] {get;} This property expects as argument the index of the table in the DataSet.Tables collection. Here is an example: private void btnDocument_Click(object sender, EventArgs e) { dsVideoCollection = new DataSet("Videos"); tblDirectors = new DataTable(); tblDirectors.TableName = "Directors"; dsVideoCollection.Tables.Add(tblDirectors); tblVideoCategories = new DataTable("Categories"); dsVideoCollection.Tables.Add(tblVideoCategories); tblRatings = dsVideoCollection.Tables.Add(); tblActors = dsVideoCollection.Tables.Add("Actors"); tblFormats = dsVideoCollection.Tables.Add("Formats"); DataTable tbl = dsVideoCollection.Tables[3]; txtVideo.Text = tbl.TableName; } This would produce Actors
The InternalDataCollectionBase class, the parent of DataTableCollection, implements the GetEnumerator() method of the IEnumerable interface. This allows you to use a foreach loop to visit each member table of the collection. Once you have reached a table in the collection, you can access any of its public properties or methods. Here is an example of applying foreach on a collection of tables of a data set to list their names: namespace VideoCollection1 { public partial class Exercise : Form { DataTable tblActors; DataTable tblFormats; DataTable tblRatings; DataTable tblDirectors; DataTable tblVideoCategories; DataSet dsVideoCollection; public Exercise() { InitializeComponent(); } private void btnDocument_Click(object sender, EventArgs e) { dsVideoCollection = new DataSet("Videos"); tblDirectors = new DataTable(); tblDirectors.TableName = "Directors"; dsVideoCollection.Tables.Add(tblDirectors); tblVideoCategories = new DataTable("Categories"); dsVideoCollection.Tables.Add(tblVideoCategories); tblRatings = dsVideoCollection.Tables.Add("Ratings"); tblActors = dsVideoCollection.Tables.Add("Actors"); tblFormats = dsVideoCollection.Tables.Add("Formats"); foreach (DataTable tbl in dsVideoCollection.Tables) lbxVideos.Items.Add(tbl.TableName); } } } This would produce:
You can use this approach to identity a table and then perform a desired operation on it.
When using the DataTable this[int index] indexed property, if you provide an index below or beyond the number of tables in the set, the compiler would throw an IndexOutOfRangeException exception. To avoid this, you can request the index of the table. To do this, call the DataTableCollection.IndexOf() method. It is overloaded in three versions. One of the versions uses the following syntax: public virtual int IndexOf(DataTable table); This version takes as argument the variable name of the table. Here is an example of calling this method: private void btnDocument_Click(object sender, EventArgs e) { dsVideoCollection = new DataSet("Videos"); tblDirectors = new DataTable(); tblDirectors.TableName = "Directors"; dsVideoCollection.Tables.Add(tblDirectors); tblVideoCategories = new DataTable("Categories"); dsVideoCollection.Tables.Add(tblVideoCategories); tblRatings = dsVideoCollection.Tables.Add(); tblActors = dsVideoCollection.Tables.Add("Actors"); tblFormats = dsVideoCollection.Tables.Add("Formats"); int index = dsVideoCollection.Tables.IndexOf(tblActors); txtVideo.Text = index.ToString(); } This would produce 3 Instead of using the variable name of the table, you can locate it using its formal name. To do this, call the following version of the IndexOf() method: public virtual int IndexOf(string tableName);
Instead of directly locating a table, you may be interested to know whether a particular table exists in the DataSet.Tables collection. To check this, you can call the DataTableCollection.Contains() method. Its syntax is: public bool Contains(string name); This method expects the object name (not the variable name) of a table as argument. If the table exists in the collection, this method returns true. Here is an example: private void btnDocument_Click(object sender, EventArgs e) { dsVideoCollection = new DataSet("Videos"); tblDirectors = new DataTable(); tblDirectors.TableName = "Directors"; dsVideoCollection.Tables.Add(tblDirectors); tblVideoCategories = new DataTable("Categories"); dsVideoCollection.Tables.Add(tblVideoCategories); tblRatings = dsVideoCollection.Tables.Add(); tblActors = dsVideoCollection.Tables.Add("Actors"); tblFormats = dsVideoCollection.Tables.Add("Formats"); if (dsVideoCollection.Tables.Contains("Actors")) MessageBox.Show("The Actors table exists"); else MessageBox.Show("The Actors table does not exist"); if (dsVideoCollection.Tables.Contains("VideoTypes")) MessageBox.Show("The VideoTypes table exists"); else MessageBox.Show("The VideoTypes table does not exist"); } This would produce:
If you happen to have a table you don't need anymore or whose role is undefined in your application, you can delete that table. This operation is supported by the DataTableCollection.Remove() method that is overloaded with two versions. To delete a table using its variable declared name, you can use the following version: public void Remove(DataTable table); This version expects the variable name that was used to declare the DataTable object. If the table exists in the DateSet.Tables collection, it would be deleted. Here is an example: private void btnDocument_Click(object sender, EventArgs e) { dsVideoCollection = new DataSet("Videos"); tblDirectors = new DataTable(); tblDirectors.TableName = "Directors"; dsVideoCollection.Tables.Add(tblDirectors); tblVideoCategories = new DataTable("Categories"); dsVideoCollection.Tables.Add(tblVideoCategories); tblRatings = dsVideoCollection.Tables.Add(); tblActors = dsVideoCollection.Tables.Add("Actors"); tblFormats = dsVideoCollection.Tables.Add("Formats"); foreach (DataTable tbl in dsVideoCollection.Tables) lbxOriginalVideos.Items.Add(tbl.TableName); dsVideoCollection.Tables.Remove(tblVideoCategories); foreach (DataTable tbl in dsVideoCollection.Tables) lbxNewVideos.Items.Add(tbl.TableName); } This would produce:
To delete a table using its object name, you can use the following version of the DataTableCollection.Remove() method: public void Remove(string name); This method expects the formal name of the table as argument. If a table exists under that name, it would be deleted. Here is an example: private void btnDocument_Click(object sender, EventArgs e) { dsVideoCollection = new DataSet("Videos"); tblDirectors = new DataTable(); tblDirectors.TableName = "Directors"; dsVideoCollection.Tables.Add(tblDirectors); tblVideoCategories = new DataTable("Categories"); dsVideoCollection.Tables.Add(tblVideoCategories); tblRatings = dsVideoCollection.Tables.Add(); tblActors = dsVideoCollection.Tables.Add("Actors"); tblFormats = dsVideoCollection.Tables.Add("Formats"); foreach (DataTable tbl in dsVideoCollection.Tables) lbxOriginalVideos.Items.Add(tbl.TableName); dsVideoCollection.Tables.Remove("Categories"); foreach (DataTable tbl in dsVideoCollection.Tables) lbxNewVideos.Items.Add(tbl.TableName); } If no table with the name is found, the compiler would throw an ArgumentException exception. Once again, you should first check that a table with the undesired name exists before deleting it. If the table exists in the collection, it may not allow the user to delete it. To find out whether a table can be deleted, call the DataTableCollection.CanRemove() method. Its syntax is: public bool CanRemove(DataTable table); When calling the DataTableCollection.Remove() method, if the DataTable object passed as argument is not found, the compiler would throw either an ArgumentNullException or an ArgumentException exception. Here is an example: private void btnDocument_Click(object sender, EventArgs e) { dsVideoCollection = new DataSet("Videos"); tblDirectors = new DataTable(); tblDirectors.TableName = "Directors"; dsVideoCollection.Tables.Add(tblDirectors); dsVideoCollection.Tables.Remove("Types"); } This would produce:
Therefore, before deleting a table, you should first check its existence. To do this, you can call the DataTableCollection.Contains() method. Here is an example of calling this method before deleting a table: private void btnDocument_Click(object sender, EventArgs e) { dsVideoCollection = new DataSet("Videos"); tblDirectors = new DataTable(); tblDirectors.TableName = "Directors"; dsVideoCollection.Tables.Add(tblDirectors); tblVideoCategories = new DataTable("Categories"); dsVideoCollection.Tables.Add(tblVideoCategories); tblRatings = dsVideoCollection.Tables.Add(); tblActors = dsVideoCollection.Tables.Add("Actors"); tblFormats = dsVideoCollection.Tables.Add("Formats"); foreach (DataTable tbl in dsVideoCollection.Tables) lbxOriginalVideos.Items.Add(tbl.TableName); if (dsVideoCollection.Tables.Contains("Types")) dsVideoCollection.Tables.Remove("Types"); else MessageBox.Show("The table named Types was not found in the database"); foreach (DataTable tbl in dsVideoCollection.Tables) lbxNewVideos.Items.Add(tbl.TableName); } This would produce:
While deleting a table from the collection, the DataTableCollection fires a CollectionChanging event. After a table has been removed from the collection, the DataTableCollection fires a CollectionChanged event. As we saw earlier, both events are of type CollectionChangeEventArgs. While a table is being deleted or after it has been deleted, the action of this event is Remove.
To delete all tables of a DataSet object, you can call the DataTableCollection.Clear() method. Its syntax is: public void Clear(); Calling this method would remove all DataTable objects of the DataSet. When clearing the collection of all its tables, the DataTableCollection fires a CollectionChanging event. After the tables have been deleted, the DataTableCollection fires a CollectionChanged event. In both cases, the action of this event is Refresh. |
|
|||||||||||||||||||||
|