Friday, 27 May 2011

How to use ASP.Net DataColumn Class Properties


In ASP.NetDataColumn class enables you to add a new column inDataTable to create in-memory structure of table populated with records dynamically. ASP.Net DataColumn allows you to define the columnName, DataType, unique and auto increment properties for new column. Using DataRow class object you can add new records under different DataColumns of DataTable by creating new row dynamically. In this tutorial we will discuss the commonly used properties of ASP.Net DataColumn class object.

Creating ASP.Net DataColumn Class Object


DataColumn myDataColumn = new DataColumn();

Above C# code shows the simplest code line to create the DataColumn class object. While initializing the new instance of DataColumn object using "new" keyword it supports 4 additional constructor overloads that accept different sets of parameters to initialize the instance of DataColumn. Following are the DataColumn initialization overloads:
  1. public DataColumn(string columnName);

  2. public DataColumn(string columnName, Type dataType);

  3. public DataColumn(string columnName, Type dataType,string expr);

  4. public DataColumn(string columnName, Type dataType,string expr, MappingType type);


First ASP.Net C# DataColumn class constructor accepts 1 parameter of string type as columnName that initializes a new column with specified column name. Second overloaded constructor accepts 2 parameters, 1st parameter is same as in the previous constructor but 2nd parameter supports System.Type value that specifies the DataType for new column. Third overload accepts 3 parameters among them first two parameters are same as in second overloaded constructor that accepts 2 parameters. 3rd parameter of third overloaded constructor of DataColumn class accepts string type expression to calculate the value for the data column. Last overloaded constructor accepts 1 extra parameter than third type of constructor as MappingType. MappingType is an enum type value that specifies the type of mapping for the new column. These constructors of ASP.Net DataColumn class allows you to specify some of the properties in a single initialization C# code but instead of them there are some other properties of DataColumn that allows you to get or set their values after the creation and initialization of DataColumn class object.

ASP.Net DataColumn Class Properties


  1. AllowDBNull: Accepts bool type value (true/false) that specifies whether null values are allowed in this data column or not.

  2. AutoIncrement: Accepts bool type (true/false) that specified whether this column will increment the value automatically or not while adding a new row to the data table.

  3. AutoIncrementSeed: Accepts the number value to specify the starting value for auto increment supporting column.

  4. AutoIncrementStep: Accepts the number value to specify the increment step for auto increment supporting column.

  5. ColumnName: Accepts the string value to specify the name of column.

  6. DataType: Accepts the System.Type value to specify the data type for column to store the specified type of data in it.

  7. DefaultValue: Accepts the default value for the column, incase no value passed to the column in a new row.

  8. MaxLength: Accepts the integer type value to specify the maximum length for the text column.

  9. Unique: Accepts the bool type value to specify that the column must contain a unique in each row of datatable.

No comments:

Post a Comment