ns12345
ns12345

Reputation: 3105

Autogenerate columns null values

I want to save null values to a decimal column(allows nulls on db) on a wpf datagrid (with autogenerate columns).

It doesn't allow me to save nulls and shows red error box.

Upvotes: 0

Views: 340

Answers (3)

ns12345
ns12345

Reputation: 3105

Just add this code in AutogeneratingColumn event:

if (e.Column.ToString() == "System.Windows.Controls.DataGridTextColumn") { (((System.Windows.Controls.DataGridBoundColumn)(e.Column)).Binding).TargetNullValue = string.Empty; }

Upvotes: 0

GameAlchemist
GameAlchemist

Reputation: 19294

to which kind of database are you binding your datagrid to ? for instance if you bind it to a datatable with ado.net, the datatable is not aware of the underlying sql schema unless you update it yourself. --> see http://support.microsoft.com/kb/310128 with linq2sql it should work fine without this update, with others like MySql i don't know.

Upvotes: 0

paparazzo
paparazzo

Reputation: 45106

I suspect you are running into a problem that I had but it was a simple text box (not a datagrid). You think you are passing a null but you are really passing string.empty which is neither null nor a decimal. I fixed it with a converter to convert string.empty to null. H.B. (the same H.B.) that edited your question answered mine.

Cannot Assign a Null Value to a Nullable Int32? via Binding

Upvotes: 1

Related Questions