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
ahmad alsafiPosted Apr 24, 2015, 2:36 PM
SamioPosted May 21, 2012, 6:40 PM
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.
VulpesPosted May 21, 2012, 3:49 PM
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
SamioPosted May 21, 2012, 2:30 PM
http://blog.gfader.com/2009/02/entity-framework-why-not-use.html
I wish I could adapt it to my case.
SamioPosted May 21, 2012, 1:56 PM
I'll try something like this:
dataBindingSource1.AddNew(new value1, value2, value3, ....);
VulpesPosted May 21, 2012, 1:44 PM
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";
SamioPosted May 21, 2012, 12:52 PM
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
VulpesPosted May 21, 2012, 11:24 AM
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: