Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to programatically create a DataRowView in ADO.NET

Posted on 2009-07-13
4
Medium Priority
?
1,050 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 150 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

604 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