Solved

How to programatically create a DataRowView in ADO.NET

Posted on 2009-07-13
4
1,020 Views
Last Modified: 2013-11-08
My problem is when I want to create a new row in a typed DataTable and edit it using DataBound WPF controls. Using the (typed) DataTable method: NewTABLERow() I can get a new instance of a typed datarow for TABLE. The particular database table I'm refering to has several columns that allow Null values.
When I bind, for example, a textbox to a Propery from the (Typed) DataRow that contains a Null value, an exception is thrown (StrongTypingException).

Instead of using the typed DataRow, if I used instead a DataRowView of the typed DataRow I should be ok, because no exception would be thrown... The problem is that I'm not finding a way of creating the DataRowView out of the typed DataRow...

Thanks in advance,
Rui
0
Comment
Question by:rui_dias
  • 2
  • 2
4 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 50 total points
ID: 24849714
If you are working with String data in the typed DataSet designer, you can change the field to not throw an exception for null data.


Typed-DataSet-Designer----Null-S.png
0
 

Author Comment

by:rui_dias
ID: 24859289
That does work, thank you.

The way I ended up doing it was, using a view of the DataTable I wanted to add a DataRow to, I used the DataRowView that is returned when you add a new row to the DataView.
MyDataSet.MyDataTable dataTable = new MyDataSet.MyDataTable();
 
//Fill the data table...
 
DataView dataView = dataTable.DefaultView;
 
//To get the new DataRowView:
DataRowView dataRowView = dataView.AddNew();
dataRowView.BeginEdit();
 
//edit the dataRowView here...
 
//Accept(EndEdit) or reject(CancelEdit) the changes: if rejected 
//the DataRowView is not added to the DataTable!
 
dataRowView.EndEdit();
 
//or: dataRowView.CancelEdit();

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24859439
I usually don't go down that route of adding to the DataView with typed DataSets, since you can add to the underlying DataTable, and the changes would be reflected in the DataView.
0
 

Author Comment

by:rui_dias
ID: 24861706
That is true, but the problem is, when you create the typed dataset using the designer from a database with lots of fields that allow null, you get that "Throw Exception" behavior by default on all of them...

I was just trying to find a solution that didn't involve changing the "throw exception" behavior on all of them "by hand", in the designer. Using the views apparently works fine and has the added advantage of not being necessary to do anything to the dataset that is automatically generated using the designer.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question