Thursday, July 21, 2011

Default Values via BindingSource's AddingNew Event

I new that BindingSource's AddingNew event can handle all default value assignment, but was so frustrated that I could not find any articles/tutorials which guided me on how to use it. Although MSDN had description, but it did not have samples on how to use it. However, with few long hour searches I came to a source which enlighted me on how to use this event.

A sample code looks like this:
 
private void branchBindingSource_AddingNew(object sender, AddingNewEventArgs e)
{

//// Get data table view 
DataView dataTableView = branchBindingSource.List as DataView;


//// Create row from view
DataRowView rowView = dataTableView.AddNew();


rowView["BranchID"] = -1;
rowView["BranchCode"] = "001";
rowView["BranchDescription"] = "002";

//// Set New row
e.NewObject = rowView;

branchBindingSource.MoveLast(); 

}



 
 


11 comments:

Anonymous said...

This is an outstanding bit of work that will save me countless hours of development time. THANK YOU!!!

orlynrules said...

thanks for taking the time to post this, it's been very helpful for me

Hyderabad Jobs said...

Nice post all the information of the place there in the blog but it should have also contains some pictures or images of the place which look more attractive

Krokonoster said...

Awesome! You just saved me from spending another fruitless hour trying to figure this out!

Jason Timmins said...

Thanks for that. Very useful.

Anonymous said...

Thank you, thank you, thank you!

JulianH said...

Thank you so much! Still getting used to .NET, there is so much to it and finding the 'right' way to do things can be confusing!

Anonymous said...

Thank you, thank you, thank you! My headache is gone.

merihem said...

Thank you, was looking hours for this !!!!

RobertSF said...

Another one here who was very happy to find this page! It looks like you haven't updated the blog recently. I hope everything is well with you!

RobertSF said...

Wow, eight years later, and this is still a VERY useful post. I've actually used it, forgotten about it, and then searched for it again when I needed it again.