Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > problem while handling dataerror
 

problem while handling dataerror

some problem while handling dataerror event i have handlemy dataerror as

private void dbGridDistrict_DataError(object sender, DataGridViewDataErrorEventArgs e)

{

if (e.Context == DataGridViewDataErrorContexts.Commit)

{

if ((e.Exception) is ConstraintException)

{

this.dbGridDistrict.Rows[e.RowIndex].ErrorText = " Cannot contain duplicate value";

MessageBox.Show("primary key violation");

e.Cancel = true;

}

if ((e.Exception) is NoNullAllowedException)

{

this.dbGridDistrict.Rows[e.RowIndex].ErrorText = " All the fields are requierd";

MessageBox.Show("Null value cannot be saved");

e.Cancel = true;

}

}

}

/////my save button fire as follows

private void btnSave_Click(object sender, EventArgs e)

{

this.Validate();

this.dISTRICTCODEBindingSource.EndEdit();

this.dISTRICTCODETableAdapter.Update(this.districtInfo.DISTRICTCODE);

MessageBox.Show("The record applied and saved", "EMUGAS:Record Saved,Information",

MessageBoxButtons.OK, MessageBoxIcon.Information);

}

this work fine if i handle e.Cancle = false and click save button it delete lastrow. But if i handle e.Cancle=True then click save button it shows the dataerror message and then it hangs(not responding)..

no-cool  Tuesday, July 10, 2007 3:09 PM

Hi no-cool,

Yes, this is the way DataGridView.DataError event works. When you set e.Cancel to true,the event will be canceled. For further information, check this article on MSDN

Hope this helps.

Regards

Rong-Chun Zhang  Thursday, July 12, 2007 2:09 AM

Hi no-cool,

Set the Button.CauseValidationg to false. See the Sample below.

Code Snippet

NewDGV

public partial class DGVDataError : Form

{

public DGVDataError()

{

InitializeComponent();

}

DataTable dt = new DataTable();

private void DGVDataError_Load(object sender, EventArgs e)

{

dt.Columns.Add("aa");

dt.Columns.Add("bb");

dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };

for (int i = 0; i < 10; i++)

{

dt.Rows.Add(i.ToString(), "bb" + i);

}

this.dataGridView1.DataSource = dt;

this.dataGridView1.DataError += new DataGridViewDataErrorEventHandler(dataGridView1_DataError);

this.button1.CausesValidation = false;

this.button1.Click += new EventHandler(button1_Click);

}

void button1_Click(object sender, EventArgs e)

{

this.Validate();

MessageBox.Show("Button is clicked");

}

void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)

{

e.Cancel = true;

this.dataGridView1.CurrentRow.ErrorText = "sdfsaffsdf";

}

}

Hi no-cool,

Yes, this is the way DataGridView.DataError event works. When you set e.Cancel to true,the event will be canceled. For further information, check this article on MSDN

Hope this helps.

Regards

Rong-Chun Zhang  Thursday, July 12, 2007 2:09 AM

thanks Rong-Chun Zhang

but i could not get it working let me provide u my full code

//form save button

private void btnSave_Click(object sender, EventArgs e)

{

try

{

this.Validate();

this.dONARCODEBindingSource.EndEdit();

result = MessageBox.Show("Do you want to Save Records", "EMUGASTongue Tiedave Confirm,Question",MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (result == DialogResult.Yes)

{

this.dONARCODETableAdapter.Update(this.dsDonartab.DONARCODE);

MessageBox.Show("The records are applied and saved.", "EMUGAS:Record Saved,Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

else

{

return;

}

}

catch (ConstraintException ex)

{

MessageBox.Show(ex.Message.ToString());

}

//tool bar save button

private void toolbtnSave_Click(object sender, EventArgs e)

{

this.Validate();

if (!iserror)

this.btnSave_Click(null, null);

}

//data error handling

private void dgvDonar_DataError(object sender, DataGridViewDataErrorEventArgs e)

{

this.iserror = true;

if (e.Context == DataGridViewDataErrorContexts.Commit)

{

if ((e.Exception) is ConstraintException)

{

this.dgvDonar.Rows[e.RowIndex].ErrorText = " Cannot contain duplicate value";

MessageBox.Show("primary key violation"); //data error messagebox

e.Cancel = true;

}

if ((e.Exception) is NoNullAllowedException)

{

this.dgvDonar.Rows[e.RowIndex].ErrorText = " All the fields are requierd";

MessageBox.Show("Null value cannot be saved"); //data error messagebox

e.Cancel = true;

}

}

}

Now here my toolbtnSave works fine . Whenever error occurs it raise the dataerror messagebox and messagebox.ok click it returns to datagridview and show dataerror text for input correction. My form closing event also works similary. But my problem with btnSave button it shows dataerror messagebox and when I click messagebox.ok it goes to not responding phase(app simplyhangs do not show any error).

Two buttons areshowing twodifferent nature again eventhough calling same method what might be wrong..

no-cool  Saturday, July 14, 2007 2:56 PM

Hi no-cool,

Nothing is wrong. A button needs focus to trigger the Button.Click event. After the DataError occurs, if you set e.Cancel to true, the focus will keep in DataGridView, so the button cannot get the focus to trigger the click, while a toolstrip does not need focus. For a workaround, check this thread.

Hope this helps.

Regards

Rong-Chun Zhang  Monday, July 16, 2007 7:12 AM

thank Rong- Chun Zhang now i know now what the problem is.........the workaround thread is just for checking wheather the dataerror occured or not ( if data error occured than do not allow toolstrip button trigger normally it worked because itdoes not require a focus to trigger the event and we wrote the condition (!iserror)in toolbtnSave_Click it fired and worked).

But problem here is

1. where can i fire if(!iserror) condition ?? if i fire this is btnSave_Click event this will never executeif i sete.Cancle=ture cause the focus will be atdatagridview...and the Save Button needs focus to fire the event.....

2.how could i bring back the foucus in save button after i set e.Cancle=ture in dataerror or some other ways to fire the button click after dataerror occurs.

please help soon i stuck here

no-cool  Tuesday, July 17, 2007 8:15 AM

Hi no-cool,

1. where can i fire if(!iserror) condition ??

We can change the value of iserror to false in the CellValidating event. Because the CellValidating will be trigger earlier than the DataError event. If the DataError won't trigger, the iserror will keep false.

Code Snippet

void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)

this.iserror = false;

I am afraid you cannot set the focus to save button if you set e.Cancle = true.

In normal ways, we should preventthe data from being saved to the database when error occurs, I wanna why you try to save data when error occurs.

Hope this helps.

Regards

Rong-Chun Zhang  Tuesday, July 17, 2007 8:59 AM

once again thanks mr Zhang but this is not what i was looking for

any way ialready have this line in my code before starting this thread and still there ...........

void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)

this.iserror = false;

Hi no-cool,

Set the Button.CauseValidationg to false. See the Sample below.

Code Snippet

NewDGV

public partial class DGVDataError : Form

{

public DGVDataError()

{

InitializeComponent();

}

DataTable dt = new DataTable();

private void DGVDataError_Load(object sender, EventArgs e)

{

dt.Columns.Add("aa");

dt.Columns.Add("bb");

dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };

for (int i = 0; i < 10; i++)

{

dt.Rows.Add(i.ToString(), "bb" + i);

}

this.dataGridView1.DataSource = dt;

this.dataGridView1.DataError += new DataGridViewDataErrorEventHandler(dataGridView1_DataError);

this.button1.CausesValidation = false;

this.button1.Click += new EventHandler(button1_Click);

}

void button1_Click(object sender, EventArgs e)

{

this.Validate();

MessageBox.Show("Button is clicked");

}

void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)

{

e.Cancel = true;

this.dataGridView1.CurrentRow.ErrorText = "sdfsaffsdf";

}

}

thanks Rong-Chun Zhang , now it works fine

this.button1.CausesValidation = false; //does the trick and that is what i was looking for.

no-cool  Tuesday, July 24, 2007 8:36 AM

You can use google to search for other answers

Custom Search

More Threads

• Table Adapter Insert Problems
• New record in datagrid
• DataGrid problem!
• Problem with Microsoft.Jet.OLEDB.4.0 Provider
• Setting initial values when adding a datatgridview row
• dataview windows form
• Null Reference Exception when adding column to Column Collection
• Get dgv cell style back
• Using VB Express to connect non Sql or Access Database....
• Binding A comboBox