Bug in DataGrid.CommitEdit (.Net v1.1)

G

Guest

After many hours of debugging, I've finally come to the conclusion that there
is a bug in DataGrid's CommitEdit.

I have the following situation: I've a custom DataGridColumnStyle (for a
ComboBox, who hasn't done one of these?). If no selection is made (e.g.
SelectedIndex == -1), I want to abort the commit. Problem is, when my
DataGridColumnStyle.Commit override is called and returns false,
DataGrid.CommitEdit causes a NullReferenceException to be thrown.

I'm using the following code:

/// <code>
bool retValue = false;

try
{
int index = this._Combo.SelectedIndex;
if ( index >= 0 )
{
object value;
if ( this._Combo.ValueMember == null )
value = this._Combo.SelectedItem;
else
value = this._Combo.SelectedValue;
this.SetColumnValueAtRow( source, rowNum, value );
retValue = true;
}
}
catch(Exception)
{}

this.EndEdit();
return retValue;
/// </code>

I googled some posts and saw people having the same problem, but I saw no
explanations. Not satisified (and lacking a solution), I fired up WinDbg,
SOS and Reflector.

The disassembler DataGrid.CommitEdit is:
/// <code>
private bool CommitEdit()
{
if ((!this.gridState[0x8000] && !this.gridState[0x4000]) ||
this.gridState[0x10000])
{
return true;
}
this.gridState[0x10000] = true;
if (this.editColumn.ReadOnly || this.gridState[0x100000])
{
if (base.ContainsFocus)
{
if (this.gridState[0x800])
{
this.FocusInternal();
}
this.editColumn.ConcedeFocus();
if ((this.gridState[0x800] && base.CanFocus) &&
!this.Focused)
{
this.FocusInternal();
}
}
this.gridState[0x10000] = false;
return true;
}
bool flag1 = this.editColumn.Commit(this.ListManager, this.currentRow);
this.gridState[0x10000] = false;
if (flag1)
{
this.gridState[0x8000] = false;
}
return flag1;
}
/// </code>

In WinDbg, I broke on the exception thrown and took a look at the object
dump for the datagrid. Turns out that the first if statement evaluates to
false. On the second if statement (if (this.editColumn.ReadOnly ...),
DataGrid.editColumn is null, hence the exception.

The full stack trace leading up to the exception is:
System.NullReferenceException: Object reference not set to an instance of an
object.
at System.Windows.Forms.DataGrid.CommitEdit()
at System.Windows.Forms.DataGrid.EndEdit()
at System.Windows.Forms.DataGrid.Edit(String instantText)
at System.Windows.Forms.DataGrid.Edit()
at System.Windows.Forms.DataGrid.set_CurrentCell(DataGridCell value)
at System.Windows.Forms.DataGrid.OnMouseDown(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)


The exact context of the crash is, I'm editting my combo box column, then
click another column (with a selected index == -1, causing my Commit to
return false). Oddly enough, if I cancel editting by clicking a control
outside of the data grid (e.g. not causing another cell to gain focus), no
error occurs until the data grid once again has a cell gain focus.

Can anyone confirm/reproduce this? If necessary, I'll throw together a
toned-down application to reproduce the error.

Thanks,
Nathan
 

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

Top