Column maintenance consists of adding one or more columns to a table, identifying an existing column in a table, looking for a column in a table, deleting one column or deleting all columns of a table. All these operations are easily supported by various classes of the System.Data namespace.
You are probably now familiar with the relationships among the data set, the table and the columns. Just in case:
When using the information stored in a table as we will learn when studying records, sometimes you will need to identify the table that owns a particular column you are accessing. This information can be provided by the Table property of the DataColumn class.
Once again, remember that the group of columns of a table is an object of type DataColumnCollection. To access a column, the DataColumnCollection class is equipped with an indexed property (named Item). Here is an example of using it: namespace VideoCollection1 { public partial class Exercise : Form { DataTable dtVideos; DataColumn[] colVideos; DataSet dsVideoCollection; public Exercise() { InitializeComponent(); } private void btnDocument_Click(object sender, EventArgs e) { colVideos = new DataColumn[7]; colVideos[0] = new DataColumn("Title"); colVideos[1] = new DataColumn("Director"); colVideos[2] = new DataColumn("YearReleased"); colVideos[3] = new DataColumn("Length"); colVideos[4] = new DataColumn("Rating"); colVideos[5] = new DataColumn("Format"); colVideos[6] = new DataColumn("Category"); dtVideos = new DataTable("Videos"); dtVideos.Columns.AddRange(colVideos); dsVideoCollection = new DataSet("VideoCollection"); dsVideoCollection.Tables.Add(dtVideos); for (int i = 0; i < dtVideos.Columns.Count; i++) lbxVideos.Items.Add(dtVideos.Columns[i].ColumnName); } } }
The DataColumnCollection class implements the GetEnumerator() method of the IEnumerable interface. This allows you to enumerate the columns of a table using foreach. Here is an example of using it: private void btnDocument_Click(object sender, EventArgs e) { colVideos = new DataColumn[7]; colVideos[0] = new DataColumn("Title"); colVideos[1] = new DataColumn("Director"); colVideos[2] = new DataColumn("YearReleased"); colVideos[3] = new DataColumn("Length"); colVideos[4] = new DataColumn("Rating"); colVideos[5] = new DataColumn("Format"); colVideos[6] = new DataColumn("Category"); dtVideos = new DataTable("Videos"); dtVideos.Columns.AddRange(colVideos); dsVideoCollection = new DataSet("VideoCollection"); dsVideoCollection.Tables.Add(dtVideos); foreach(DataColumn col in dtVideos.Columns) lbxVideos.Items.Add(col.ColumnName); }
To check whether a table contains a certain column, you can call the Contains() method of the DataColumnCollection class. Its syntax is: public bool Contains(string name); This method takes as argument the object name of a column. When the method is called, the compiler would look for that column in the table. If the table contains that column, the method returns true. Otherwise it returns false.
If you happen to have an undesired column in a table, you can delete it. To perform this operation, the DataColumnCollection class provides the Remove() method. This method is overloaded in two versions. One of them uses the following syntax: public void Remove(string name); This method expects the object name of a column as argument. If the table has that column, the column would be deleted. Here is an example: namespace VideoCollection1 { public partial class Exercise : Form { DataColumn colTitle; DataColumn colDirector; DataColumn colLength; DataColumn colFormat; DataColumn colRating; DataTable tblVideos; public Exercise() { InitializeComponent(); } private void btnDocument_Click(object sender, EventArgs e) { tblVideos = new DataTable("Video"); colTitle = new DataColumn("Title"); tblVideos.Columns.Add(colTitle); colDirector = new DataColumn("Director"); tblVideos.Columns.Add(colDirector); colLength = new DataColumn("Length"); tblVideos.Columns.Add(colLength); colFormat = new DataColumn("Format"); tblVideos.Columns.Add(colFormat); colRating = new DataColumn("Rating"); tblVideos.Columns.Add(colRating); lbxVideos.Items.Clear(); lbxNewVideos.Items.Clear(); foreach (DataColumn col in tblVideos.Columns) lbxVideos.Items.Add(col.ColumnName); tblVideos.Columns.Remove("Format"); foreach (DataColumn col in tblVideos.Columns) lbxNewVideos.Items.Add(col.ColumnName); } } }
If there is no column with the object name passed as argument to the DataColumnCollection.Remove() method, the compiler would throw an ArgumentException exception. Here is an example of calling it: private void btnDocument_Click(object sender, EventArgs e) { tblVideos = new DataTable("Video"); lbxVideos.Items.Clear(); lbxNewVideos.Items.Clear(); foreach (DataColumn col in tblVideos.Columns) lbxVideos.Items.Add(col.ColumnName); tblVideos.Columns.Remove("Category"); foreach (DataColumn col in tblVideos.Columns) lbxNewVideos.Items.Add(col.ColumnName); } This would produce:
As this can only be undesirable, the DataColumnCollection class allows you to check the existence of the column on the table. This is done by calling the Contains() method. Even if the table contains that column, it may not allow the column to be be deleted. For this reason, you should first check that the table allows that the column be deleted. To assist you with checking this, the DataColumnCollection class is equipped with the CanRemove() method. Its syntax is: public bool CanRemove(DataColumn column);
The columns of a table are arranged in an indexed list with the first (the most left) column at index 0, the second (from left) at index 1, and so on. To delete a column based on its index, you can call the DataColumnCollection.RemoveAt() method. Its syntax is: public void RemoveAt(int index); The index of the column is passed to this method. When calling this method, make sure you pass a valid index that is an integer greater than or equal to 0 but less than the DataColumnCollection.Count - 1. If you pass a negative index or a number >= DataColumnCollection.Count, the compiler would throw an IndexOutOfRangeException exception. If you know the object name of the column, you can first get its index by calling the DataColumnCollection.IndexOf() method and then pass its returned value to the RemoveAt() method.
To delete all columns from a table, you can call the DataColumnCollection.Clear() method. Its syntax is: public void Clear(); After this method has been called, all columns from the table are deleted. |
|
|||||||||||||||||||
|