Tuesday, 21 April 2009

New row default values in DataGridView

I was looking for a nice way of displaying default values in a datagridview when a user creates a new row.


At first I looked at settings in the binding source but there wasn't anything that stood out. Then I looked att he table adapter again there wasn't anything I could use.


Finally I found that it was an event in dataGridView that was required. The event is called 'DefaultValuesNeeded'. It can be used to specify default values for any row in a DataGridView. It is fired when you create a new row in a DatagridView.


Below is a C# example from http://msdn.microsoft.com/en-us/library/b22t666e.aspx


private void dataGridView1_DefaultValuesNeeded(object sender,
System.Windows.Forms.DataGridViewRowEventArgs e)
{
e.Row.Cells["Region"].Value = "WA";
e.Row.Cells["City"].Value = "Redmond";
e.Row.Cells["PostalCode"].Value = "98052-6399";
e.Row.Cells["Region"].Value = "NA";
e.Row.Cells["Country"].Value = "USA";
e.Row.Cells["CustomerID"].Value = NewCustomerId();
}

Very useful and it is something I will be using a lot.

2 comments:

for IT the said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru Dot Net Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

john kevin said...

I have to voice my passion for your kindness giving support to those people that should have guidance on this important matter.

java training in bangalore