DataGridView Bug

H

homawong

This bug is very obvious but as I search for it on Internet,
suprisingly I only found it is mentioned in one instance and without a
reply about the issue. That original post is more complicated and I'm
able to trim it down to its simplest form.

All you need is a DataSet, Form and DataGridView.

1. Create a form
2. Create a DataSet with one table with two or more column, say MyTable
[ Col1, Col2].
3. Add a DataGridView to the form and bind to the DataSet

When you run the program:

1. Edit one of the column. Which one doesn't matter, but for demo, lets
say the first column.
2. Tab or click to another column of the same row.
3. Press [Esc] to cancel the edit.

Supposingly, the DataGridView should cancel edit and leave with one row
(the detached row). But instead, DataGridView insert two empty rows to
the DataSet.MyTable, so you have three rows displayed in the
DataGridView (two Added and one detached).

This happens only when the DataGridView is empty, which means it only
have one (the detached) row.

When there are rows in the DataSet already, DataGridView behaves
properly.



Homa Wong
 
H

homawong

A work around for this issus is provided by Steve Hirscher who
experienced the same problem with custom data object.

Here is his response:

I was never able to find a fix for it. Another developer here ran into
the same problem and I'm pretty convinced now that its simply a bug in
the framework. What I had to end up doing as a workaround was to
capture the KeyDown event on the DataGridView and check if the user was
pressing the escape key and the current row is the first row. If so I
just canceled the event which meant that the user couldn't cancel the
entry of the first row, they had to delete it themselves instead. This
still allowed them to cancel the edit of a single cell by pushing
escape since that KeyDown event was raised by the DataGridViewColumn,
not the grid itself. Here's the code I used for the workaround:

void MyGridView_KeyDown( object sender, KeyEventArgs e)
{
// HACK: Workaround for DataGridView bug
if (e.KeyCode == Keys.Escape && MyGridView.CurrentRow !=
null && MyGridView .CurrentRow.Index == 0)
{
e.SuppressKeyPress = true;
e.Handled = true;
}
}

Not the greatest solution but it has at least kept an unhandled
exception from popping up for the time being.


For me, I change it to,

void MyGridView_KeyDown( object sender, KeyEventArgs e)
{
// HACK: Workaround for DataGridView bug
if (e.KeyCode == Keys.Escape && MyGridView.CurrentRow !=
null && MyGridView .CurrentRow.Index == 0)
{
e.SuppressKeyPress = true;
e.Handled = true;
MyGridViewBindingSource.CancelEdit();
}
}

which works for DataSet Binding.



Can anyone confirm this bug/fix?

Homa Wong
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top