Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Problems with CancelEdit in a DataGridViewRow
 

Problems with CancelEdit in a DataGridViewRow

Hi,

I created a little form with a TabControl on it and2 TabPages.

On the first page i added a DataGridView,bounded to a store procedure to display rows from a table (not editable).

On de 2nd page i added TextBoxs bounded to the CellsRows of the DataGridView

When I change to the 2nd page the textbox show de contens of the Cells from the DataRow selected on the DataGridView

For this 2nd pagei added an event handler for Validating the textbox on Edit Mode

To edit therow I added a toolstrip button to enable thetextboxs on the TabPage2, CauseValidationand make the BeginEdit of the row.

When I disable the EditMode I force the event to Validate the values of the textboxs and show a message if there is an error. Then make the CancelEdit of the row and disable the textboxs and CauseValidation.

The first time I Enable and Disable the EditMode whit an error (empty value) the programworks well and the textboxs show the contens of the original values.

But when I repeat the secuenceagain (enable and disable the EditMode with the same error) the textbox remain empty and don't get the original value.

I don't know if I doing something wrong
Seems that the 2nd time it miss the original values of the row to refresh the textboxs.

Is there any solution to this ?

Here'smy code (at least the part that relates to the problem)

thank you in advance for any help

private void BindingNavigatorEditItem_Click(object sender, EventArgs e) 

{

if (this.blEdicion) 

{
 

if (Validate()) 

{

this.RowView.DataGridView.EndEdit();

}

else

{

this.RowView.DataGridView.CancelEdit();

Validate();

} 

this.tabPage2.CausesValidation = false; 
this.GroupBox1.CausesValidation = false; 
this.GroupBox1.Enabled = false; 
this.blEdicion = false; 

}

else

{

this.blEdicion = true; 
this.RowView = DataGridView1.CurrentRow; 
this.RowView.DataGridView1.BeginEdit(this.blEdicion);
this.tabPage2.CausesValidation = true; 
this.GroupBox1.CausesValidation = true; 
this.GroupBox1.Enabled = true; 
this.TextBox1.Select(); 

}

private new bool Validate() 

{
this.blAllOk = true; 
this.tabPage2_Validating(this, new CancelEventArgs()); 
return this.blAllOk; 

}

private void tabPage2_Validating(object sender, CancelEventArgs e) 

{

 try

{

if (this.TextBox1.Text == "") 

{

 throw new ArgumentNullException("Empty Value"); 

}
else

{

 this.ErrorProvider1.SetError(this.TextBox1, ""); 

}

if (this.TextBox2.Text == "") 

{

throw new ArgumentNullException("Empty Value"); 

}

else

{

this.ErrorProvider1.SetError(this.TextBox2, ""); 

}

}

catch (System.Exception ex) 

{

System.Windows.Forms.MessageBox.Show(ex.Message); 
e.Cancel =true;
this.blAllOk = false; 

}

}

Javier

JavierRT  Thursday, July 23, 2009 2:57 PM

Hi,

I see you want to edit the datagridviewcell content in another TabPage. Which way did you use to bind the cell content with the textbox? Like textBox1.DataBindings.Add("text",datatable01,"col01"); ? I guess this bound relationship may be changed in some place.

Unfortunately, I am not able to reproduce your issue through your code. Could you please give use a small project to test? Please upload your demo project to some server and offer us the URL. Skydrive (http://skydrive.live.com/ ) may be a good option. You can sign up 25GB free space only with your Windows Live Passport.

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Tuesday, July 28, 2009 9:57 AM

We are changing the issue type to “General Discussion�because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question�by opening the Options list at the top of the post window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Sunday, August 02, 2009 12:54 PM

You can use google to search for other answers

Custom Search

More Threads

• No Spacing Custom Validation Code
• Setting some fields BEFORE the user can add a new row ??
• Null value in datasource causing bound field to be set to null
• Starting on a specific DataRow
• custom formatting for (too) long text
• random appearance of datagridview coloumn in DataSet (even when visible is set to "False")
• DateTime.MinValue and Number Format ?
• Scroll DataGridView while dragging cell over frozen column
• Change row colour depending on a cell value
• DataGridViewTrackBarColumn