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.
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.
Anyway, thanks.
Aaron
Marked as answer by Lynn Liu - MSFT on Jan 14, 2009 08:41 AM
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
Aaron Edward...
Member
180 Points
251 Posts
Binding a GridView to a Strongly Typed DataTable
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.
Thanks.
Aaron
Aaron Edward...
Member
180 Points
251 Posts
Re: Binding a GridView to a Strongly Typed DataTable
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
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.
Anyway, thanks.
Aaron
jcard
Member
6 Points
5 Posts
Re: Binding a GridView to a Strongly Typed DataTable
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