Solved

How to programatically create a DataRowView in ADO.NET

Posted on 2009-07-13
4
1,012 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now