I have datagridview1 and databindingNavigator1 both bound to databindingSource1
I wonder how to set a value by code to a cell in datagridview1 as soon as I click on the AddNew button of the databindingNavigator.
I tried datagridview1_RowsAdded event without success.
Thanks in advance.
Loading

VulpesRank #55 · 29.2K reputationPosted May 21, 2012, 7:24 AM EDT
If you select the BindingNavigator's AddNew button in the VS designer and then, in the Properties Box, locate the Click event and double click it, a skeleton handler should open up so you can add your code:
Samio170 reputationPosted May 21, 2012, 8:52 AM EDT
In my datagridview I have a column named "TestCell", and I have already got error using the next event:
private void BtnAddNew_Click(object sender, EventArgs e)
{
datagridview.CurrentRow.Cells["TestCell"].Value = "Test";
}
Where BtnAddNew is the button within databindingNavigator dedicated to add rows.
The error is:
System.ArgumentException was unhandled
Message=the column labeled "TestCell" was not found.
Parameter Name : columnName
Source=System.Windows.Forms
ParamName=columnName
VulpesRank #55 · 29.2K reputationPosted May 21, 2012, 9:44 AM EDT
I'd see if you can locate it using its column index instead - for example if it's in the third column:
datagridview.CurrentRow.Cells[2].Value = "Test";
Samio170 reputationPosted May 21, 2012, 9:56 AM EDT
I'll try something like this:
dataBindingSource1.AddNew(new value1, value2, value3, ....);
Samio170 reputationPosted May 21, 2012, 10:30 AM EDT
http://blog.gfader.com/2009/02/entity-framework-why-not-use.html
I wish I could adapt it to my case.
VulpesRank #55 · 29.2K reputationPosted May 21, 2012, 11:49 AM EDT
I don't recall ever using this event before but here's the documentation if you want to play around with it:
http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.addingnew.aspx
Samio170 reputationPosted May 21, 2012, 2:40 PM EDT
private void BtnAddNew_Click(object sender, EventArgs e)
{
MyTable k = new MyTable();
k.Field1 = Value1;
k.Field2 = Value2;
Myentities.AddToMyTable(k);
Myentities.SaveChanges();
MyBindingSource.RaiseListChangedEvents = true;
MyBindingSource.ResetBindings(false);
}
this code worked well, data has been persisted, but could not refresh the datagridview.
ahmad alsafi2 reputationPosted Apr 24, 2015, 10:36 AM EDT