Click here to Skip to main content
16,006,006 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all.
I have a question about the BindingSource "NewRow".
I would like to check if a newly added row of a BindingSource-object is "empty".
By a "new" BindingSource "row" i mean the new row which is generated when the user clicks the yellow plus-sign (+) in the BindingSourceNavigatorbar.(AddNew-method)
And by "empty" i mean a new datarow where all the "fields" are "nullValue".
Condition for that, of course is that no default values are defined and "nullvalue" must be allowed for all the fields.

So the question is:
How can i check (f.ex. in the form_closing event) that at least one of the fields of the new row of the BindingSource is not nullvlue?

In order to keep the answer as "simple" as possible we can consider that the row to check is the current item of the BindingSource.
(knowing that the position-property of the last added row in a BindingSource is: BS.count - 1)

Thanks
Posted

You seem to be reasonably experienced, so forgive me if this is telling you something you already know.

The row returned from a BindingSource is able to be cast to a DataRowView[^].

DataRowView Has an Items property which can be indexed by both its integer position and its name.

You can therefore iterate over the DataRowView checking each item for DBNull.Value or null, whichever is applicable to your situation.
 
Share this answer
 
v2
Hi Henry Minute.
Thank you for your fast reply.
So you wrote that the rows of a BindingSource can be cast to DataRowView.
Yes that's a good thing and i use it quite often.
If i put it into code it could be something like:
Dim myDRV As DataRowView = CType(Me.myBS.Current, DataRowView)

You said i could then iterate myDRV's items.
But i can't find out how to do that!
I cant find no Items property of myDRV.
The only property that shows up in intelisense is the Item property which needs an integer or the field's name as string.

Could you please show me (with a few lines of code) how to iterate these Items? That would help me quite a lot.

I have tried it with a For/Next loop, but as i already mentioned. I cant find the collection to iterate trough.

Thank you.
 
Share this answer
 
v2
Comments
Henry Minute 9-Nov-10 17:54pm    
You are right the property is Item (not Items, as I said).
The link to DataRowView in my previous answer was broken, I have mended it now. If you follow that link and select either the Item(Int32) or Item(String) properties you will find a short example. If that is not enough help then come back to me, by adding a comment to my answer so that I get an email like you are getting now :). I only found this by accident.
luccingolo 10-Nov-10 13:31pm    
Hi Henry Minute.
So I hope you will get an email this time. Thanks to you I have at least learnd how to post an answer....
The way the guys from MSDN solved the iteration problem is not realy what I was loking for.
In the MSDN-example they get an integer value from the Datatable.

Dim colCount As Integer = view.Table.Columns.Count

But in my projekt the data I'm working with is held in a BindingSource.
The way i finally settled it is the ItemArray property of the Datarow object.
Dim myDR As DataRow = CType(Me.myBS.Current, DataRowView).Row

For Each fieldContent In myDR.ItemArray
Console.WriteLine("Item: {0}", fieldContent.ToString)
Next
But I thank you for your help. Your answers gave me the "keywords" to google for.
And last but not least.... You told me to add a comment to your answer. Thats what I'm doing right here.
But this "Submit comment-Textbox" offers no "text or code enhancement facilities". Shoudn't I use this "Add your answer here"-box below to type my comment????

Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900