Deleting Columns |
|
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 name of a column as argument. If the table has that column, the column would be deleted. Here is an example: using System; using System.Data; namespace VideoCollection { public static class Program { static DataSet dsVideos; static DataColumn colTitle; static DataColumn colDirector; static DataColumn colLength; static DataColumn colFormat; static DataColumn colRating; static DataTable tblVideos; static void CreateCollection() { dsVideos = new DataSet("Videos"); tblVideos = new DataTable("Video"); colTitle = new DataColumn("Title", Type.GetType("System.String")); tblVideos.Columns.Add(colTitle); colDirector = new DataColumn("Director", Type.GetType("System.String")); tblVideos.Columns.Add(colDirector); colLength = new DataColumn("Length", Type.GetType("System.String")); tblVideos.Columns.Add(colLength); colFormat = new DataColumn("Format", Type.GetType("System.Int16")); tblVideos.Columns.Add(colFormat); colRating = new DataColumn("Rating", Type.GetType("System.Byte")); tblVideos.Columns.Add(colRating); dsVideos.Tables.Add(tblVideos); } static void ShowTables() { int i = 1; Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); Console.WriteLine("Video Collection - Tables"); Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); foreach (DataTable tbl in dsVideos.Tables) Console.WriteLine("{0}. {1}", i++, tbl.TableName); Console.WriteLine("----------------------------"); } static void ShowColumns(string table) { int i = 1; DataTable tbl = dsVideos.Tables[table]; Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); Console.WriteLine("Video Collection - {0} Columns", table); Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); foreach (DataColumn col in tbl.Columns) Console.WriteLine("{0}. {1}", i++, col.ColumnName); Console.WriteLine("----------------------------"); } static int Main(string[] args) { CreateCollection(); int i = 1; DataTable tbl = dsVideos.Tables["Video"]; Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); Console.WriteLine("Video Collection\nColumns of the Video table"); Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); foreach (DataColumn col in tbl.Columns) Console.WriteLine("{0}. {1}", i++, col.ColumnName); Console.WriteLine("----------------------------"); tbl.Columns.Remove("Length"); Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); Console.WriteLine("Video Collection\nColumns of the Video table"); Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); i = 1; foreach (DataColumn col in tbl.Columns) Console.WriteLine("{0}. {1}", i++, col.ColumnName); Console.WriteLine("----------------------------"); return 0; } } } This would produce: =-=-=-=-=-=-=-=-=-=-=-=-=-=-= Video Collection Columns of the Video table =-=-=-=-=-=-=-=-=-=-=-=-=-=-= 1. Title 2. Director 3. Length 4. Format 5. Rating ---------------------------- =-=-=-=-=-=-=-=-=-=-=-=-=-=-= Video Collection Columns of the Video table =-=-=-=-=-=-=-=-=-=-=-=-=-=-= 1. Title 2. Director 3. Format 4. Rating ---------------------------- Press any key to continue . . . If there is no column with the name passed as argument to the DataColumnCollection.Remove() method, the compiler would throw an ArgumentException exception. 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 whose syntax is: public bool Contains(string name); Here is an example of calling it: using System; using System.Data; namespace VideoCollection { public static class Program { . . . No Change static int Main(string[] args) { CreateCollection(); int i = 1; DataTable tbl = dsVideos.Tables["Video"]; Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); Console.WriteLine("Video Collection\nColumns of the Video table"); Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); foreach (DataColumn col in tbl.Columns) Console.WriteLine("{0}. {1}", i++, col.ColumnName); Console.WriteLine("----------------------------"); if (tbl.Columns.Contains("Length")) { tbl.Columns.Remove("Length"); Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); Console.WriteLine("Video Collection\nColumns of the Video table"); Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); i = 1; foreach (DataColumn col in tbl.Columns) Console.WriteLine("{0}. {1}", i++, col.ColumnName); Console.WriteLine("----------------------------"); } return 0; } } } 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. Here is an example: using System; using System.Data; namespace VideoCollection { public static class Program { . . . No Change static int Main(string[] args) { CreateCollection(); int i = 1; DataTable tbl = dsVideos.Tables["Video"]; Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); Console.WriteLine("Video Collection\nColumns of the Video table"); Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); foreach (DataColumn col in tbl.Columns) Console.WriteLine("{0}. {1}", i++, col.ColumnName); Console.WriteLine("----------------------------"); tbl.Columns.RemoveAt(4); Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); Console.WriteLine("Video Collection\nColumns of the Video table"); Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); i = 1; foreach (DataColumn col in tbl.Columns) Console.WriteLine("{0}. {1}", i++, col.ColumnName); Console.WriteLine("----------------------------"); return 0; } } } If you pass a negative index of 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. |
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|