DataGridView Bug

Discussion in 'ASP .Net Datagrid Control' started by homawong@gmail.com, May 10, 2006.

  1. Guest

    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
     
    , May 10, 2006
    #1
    1. Advertising

  2. Guest

    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
     
    , May 11, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dbuchanan
    Replies:
    7
    Views:
    7,438
    jtsmith2
    May 23, 2006
  2. =?Utf-8?B?Rmlyb3o=?=

    Forms inheritance - datagridview

    =?Utf-8?B?Rmlyb3o=?=, Jan 6, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,442
    =?Utf-8?B?Rmlyb3o=?=
    Jan 6, 2006
  3. dbuchanan

    DataGridView Scrollbars missing

    dbuchanan, Jan 10, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    16,130
    goldberg
    Nov 18, 2008
  4. =?Utf-8?B?SmFrb2IgTGl0aG5lcg==?=

    RE: DataGridView with databound combobox

    =?Utf-8?B?SmFrb2IgTGl0aG5lcg==?=, Mar 16, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    11,293
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Apr 2, 2006
  5. hangar18
    Replies:
    0
    Views:
    10,357
    hangar18
    Mar 27, 2006
Loading...

Share This Page