Wednesday, October 13, 2010

DataColumn : Default Value and Expression

DataColumn has five constructors, each with one or more of
following parameters :

Public Sub New ( _
columnName As String, _
dataType As Type, _
expr As String, _
type As MappingType _
)

I was thinking that the third parameter is the default value
which will be assigned to the column.

e.g. If I create a column like following and add it to the table :
dt.Columns.Add(New DataColumn("IsItAStop", System.Type.GetType("System.Boolean"), "False"))

I expected that the Boolean column will be added and will have a default
value of "False", which obviuosly I will be able to change
while adding a new row to the table.



Dim dtdtdr As DataRow = dt.NewRow
Dim ck1 As CheckBox = CType(grrow.Cells(2).FindControl("test"), CheckBox)
If Not ck1 Is Nothing Then
If ck1.Checked = True Then
dtdtdr("IsItAStop") = "True"
End If
End If
dt.Rows.Add(dtdtdr)


Now I expected the column value to be "True" for the new row added.

On the contrary, I found that the value after dt.Rows.Add was always "False".

The reason was that the "expr" in the DataColumn ctor is an expression,
and not default value.

The expression is always evaluated on call to Rows.Add. The basic
purpose of this expression is for creating derived columns.

To set a default value,
use the DefaultValue property of DataColumn.




col = New DataColumn("IsItAStop", System.Type.GetType("System.Boolean"))
col.DefaultValue = "False"
dt.Columns.Add(col)

No comments:

Post a Comment

How to check local and global angular versions

 Use the command ng version (or ng v ) to find the version of Angular CLI in the current folder. Run it outside of the Angular project, to f...