DataGridView problem-

  • Thread starter Thread starter Ogmios
  • Start date Start date
O

Ogmios

Hi to All,

I have DataGridView control bound through databindingsource to the SQLServer
2005.
The problem is when I add or edit data, situation in the database is always
step behind form situation in the grid.

I use

exampleTableAdapter.Update(exampleDataTable);

for saving and editing.. Update method is implemented good.

If I use that same line of code inside event handler of some button
everything works well but in RowLeave event it will ignore me first time and
save second time when called!!!

I would appreciate any help.

Thanx in advance.

Ognjen
 
Are you calling the EndEdit and Validate events before calling Update on
your table adapter??

Robin S.
 
Hi Robin,

well, I call only CellValidating Event, but it is not implemented in this
moment. I don't understand why it is important?

The relevant piece of code is....


private void dataGridView1_DefaultValuesNeeded(object sender,
DataGridViewRowEventArgs e)

{

e.Row.Cells["TerminiGrupaID"].Value = listBox1.SelectedValue;

e.Row.Cells["Datum"].Value = now;

e.Row.Cells[0].Value = "12:00";

}

private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)

{

//if (dataGridView1.Columns[e.ColumnIndex].Name == "DoktorIme")

//{

// if (String.IsNullOrEmpty(e.FormattedValue.ToString()))

// {

// dataGridView1.Rows[e.RowIndex].ErrorText =

// "...";

// e.Cancel = true;

// }

//}

}

private void dataGridView1_RowLeave_1(object sender,
DataGridViewCellEventArgs e)

{



terminiAdapter.Update(terminiTable);

//dataGridView2.DataSource = terminiTable;




}



Ognjen
 
Are you using a BindingSource? You said you were, but then your code looks
like you're not. If you are, you need to call EndEdit to push the changes
down into your data source before saving the changes to the database.

Robin S.
-----------------------
Ogmios said:
Hi Robin,

well, I call only CellValidating Event, but it is not implemented in this
moment. I don't understand why it is important?

The relevant piece of code is....


private void dataGridView1_DefaultValuesNeeded(object sender,
DataGridViewRowEventArgs e)

{

e.Row.Cells["TerminiGrupaID"].Value = listBox1.SelectedValue;

e.Row.Cells["Datum"].Value = now;

e.Row.Cells[0].Value = "12:00";

}

private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)

{

//if (dataGridView1.Columns[e.ColumnIndex].Name == "DoktorIme")

//{

// if (String.IsNullOrEmpty(e.FormattedValue.ToString()))

// {

// dataGridView1.Rows[e.RowIndex].ErrorText =

// "...";

// e.Cancel = true;

// }

//}

}

private void dataGridView1_RowLeave_1(object sender,
DataGridViewCellEventArgs e)

{



terminiAdapter.Update(terminiTable);

//dataGridView2.DataSource = terminiTable;




}



Ognjen


RobinS said:
Are you calling the EndEdit and Validate events before calling Update on
your table adapter??

Robin S.
 
I use binding source and I actually did call EndEdit before but it didn't
help?!?!
Now I use DataTable.Rows[index].EndEdit() and it helps.

I have tried all other combination including BindingSource.EndEdit(),
DataGridView.EndEdit() but this only that one works.

Thnx anyway

Ognjen
RobinS said:
Are you using a BindingSource? You said you were, but then your code looks
like you're not. If you are, you need to call EndEdit to push the changes
down into your data source before saving the changes to the database.

Robin S.
-----------------------
Ogmios said:
Hi Robin,

well, I call only CellValidating Event, but it is not implemented in this
moment. I don't understand why it is important?

The relevant piece of code is....


private void dataGridView1_DefaultValuesNeeded(object sender,
DataGridViewRowEventArgs e)

{

e.Row.Cells["TerminiGrupaID"].Value = listBox1.SelectedValue;

e.Row.Cells["Datum"].Value = now;

e.Row.Cells[0].Value = "12:00";

}

private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)

{

//if (dataGridView1.Columns[e.ColumnIndex].Name == "DoktorIme")

//{

// if (String.IsNullOrEmpty(e.FormattedValue.ToString()))

// {

// dataGridView1.Rows[e.RowIndex].ErrorText =

// "...";

// e.Cancel = true;

// }

//}

}

private void dataGridView1_RowLeave_1(object sender,
DataGridViewCellEventArgs e)

{



terminiAdapter.Update(terminiTable);

//dataGridView2.DataSource = terminiTable;




}



Ognjen


RobinS said:
Are you calling the EndEdit and Validate events before calling Update on
your table adapter??

Robin S.
---------------------------------
Hi to All,

I have DataGridView control bound through databindingsource to the
SQLServer 2005.
The problem is when I add or edit data, situation in the database is
always step behind form situation in the grid.

I use

exampleTableAdapter.Update(exampleDataTable);

for saving and editing.. Update method is implemented good.

If I use that same line of code inside event handler of some button
everything works well but in RowLeave event it will ignore me first
time and save second time when called!!!

I would appreciate any help.

Thanx in advance.

Ognjen
 
Hi,

Ogmios said:
Hi to All,

I have DataGridView control bound through databindingsource to the
SQLServer 2005.
The problem is when I add or edit data, situation in the database is
always step behind form situation in the grid.

I use

exampleTableAdapter.Update(exampleDataTable);

for saving and editing.. Update method is implemented good.

If I use that same line of code inside event handler of some button
everything works well but in RowLeave event it will ignore me first time
and save second time when called!!!

RowLeave fires too soon for that, have you tried handling RowValidated
instead, can't say i tested this extensively, just an idea, i don't think
you'll need to call EndEdit then....

HTH,
Greetings
 
Back
Top