Click here to Skip to main content
14,299,406 members
Rate this:
Please Sign up or sign in to vote.
See more:
i have one datatable with some rows in it.
Now i want to add defaultvalue to it, iam having column name.
i have tried using default value. but it is not working.

How to update the column value with out using loop?

i have given code below

 if (CommonDeclaration.filterstatus == "Full")
                    {
                        dtSource.Columns["Transaction Type"].DefaultValue = "F";
}


What I have tried:

if (CommonDeclaration.filterstatus == "Full")
                    {
                        dtSource.Columns["Transaction Type"].DefaultValue = "F";
}
Posted
Updated 13-Mar-19 3:31am
Comments
#realJSOP 22-Mar-18 8:00am
   
"It is not working is not nearly enough info to allow us to help you.
Rate this:
Please Sign up or sign in to vote.

Solution 1

There are three obvious routes open to you:

1) Learn to make the default value in the database table work (PREFERRED!)
2) Create an AFTER INSERT trigger on the table (if 1 too hard, this is well beyond you).
3) When sending data to the table, initialize the entry for that value on the C# side and then, unless you change it, you'll be inserting that initialized (=default) value.

More help than this cannot be given because we're left basically clueless on what your doing to what table, &etc.

Also, look here[^]

   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

Faced the same issue, was not updating default value for existing column but was working if we add new column and assign default value. So I first removed column to have default value and readded it and then assgined default value. This worked for me.


DataTable dt = new DataTable();
string strColName = "ColumnToHaveDefaultValue";
DataColumn colNew = new DataColumn(strColName, typeof(string));
colNew.DefaultValue = "Amit Jaiswal";

dt.Columns.Remove(strColName);

dt.Columns.Add(colNew);
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100