Click here to Skip to main content
15,886,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

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[^]

 
Share this answer
 
v2
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);
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900