Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Nov 06, 2012 09:02 AM by jcard
Jan 08, 2009 09:41 PM|LINK
Is there a way to bind a GridView to a strongly typed dataTable, without having the dataTable throw an exception every time one of the values is null? With a non-typed DataTable, the GridView will just display blank cells for any null values. But with
a strongly-typed dataTable, the DataTable itself raises the exception. With string fields, you can set the NullValue property to (Empty), but you can only do that with strings.
Of course I know I'm supposed to bind to a business object and not directly to a DataTable, but this is a quick and dirty job, and I was hoping to save some time.
Jan 09, 2009 03:33 PM|LINK
It's weird that no one has responded to this. It seems like it should be so easy.
Anyway, I found a workaround, and that's to simply cast it to a regular data table before calling DataBind(). Something like
DataGrid1.DataSource = (DataTable)MyTypedDataTable;
So it will work as expected. It still seems to me though that the Typed DataTable should be able to be made more flexible for data binding. I'm sure I'm missing something.
Nov 06, 2012 09:02 AM|LINK
I know I'm floating a really old thread here, but maybe other people are running into this..
I think the exception is coming from the code in the YourDataTable.Designer.cs file, that is governed by the particular DataColumn's property setting for NullValue?
Make a new dataset, new datatable, new string column. Click the column itself and see the properties window grid entry "NullValue" - by default this is "throw exception" - if you attempt to get a string that is DBNull, the code in the dataset designer file
(generated by tool) throws an exception. Change this setting on a per-column basis, to Empty to return an empty string instead of throwing an exception