[Mono-dev] Patch for DataGridView crash after Columsn.Clear

jingnan si jingnan.si at gmail.com
Wed Nov 12 20:35:26 EST 2008


Hi,
  First add several columns to DataGridView, then call
DataGridView.Columns.Clear, and re-add all the columns again, Call
DataGridView.Rows.Add(), the application will crash.
  After debug the mcs code, I found when DataGrideViewColumn added, the
DataGridView.RowTemplate will add correspond cell of the column, but when
column removed, it will not update the RowTemplate.
  I create a simple patch to make my application work.

Regards,

diff -r
mono-2.0/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewColumnCollection.cs
original/mono-2.0/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewColumnCollection.cs
105,108c105
<             //list.Clear();
<             while(Count > 0)
<                 RemoveAt(0);
<
---
>             base.List.Clear ();
202d198
<             DataGridView.OnColumnRemovedInternal (new
DataGridViewColumnEventArgs (dataGridViewColumn));
219,220c215
<             DataGridView.OnColumnRemovedInternal (new
DataGridViewColumnEventArgs (col));
<             OnCollectionChanged (new CollectionChangeEventArgs
(CollectionChangeAction.Remove, col));
---
>             OnCollectionChanged (new CollectionChangeEventArgs
(CollectionChangeAction.Add, col));
diff -r
mono-2.0/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridView.cs
original/mono-2.0/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridView.cs
1033,1034d1032
<                 rowTemplate.SetDataGridView(this);
<
3512d3509
<                 RowTemplate.Cells[RowTemplate.Cells.Count -
1].SetColumnIndex(e.Column.Index);
3650,3678d3646
<         internal void OnColumnRemovedInternal(DataGridViewColumnEventArgs
e)
<         {
<             if (e.Column.Index >= 0) {
<                 foreach(DataGridViewCell cell in RowTemplate.Cells) {
< //System.Console.WriteLine("Column:" + e.Column.Index + "," +
cell.ColumnIndex);
<                     if (cell.ColumnIndex == e.Column.Index) {
<                         RowTemplate.Cells.Remove(cell);
<                         break;
<                     }
<                 }
<
<                 foreach (DataGridViewRow row in Rows) {
<                     foreach(DataGridViewCell cell in row.Cells) {
<                         if (cell.ColumnIndex == e.Column.Index) {
<                             RowTemplate.Cells.Remove(cell);
<                             break;
<                         }
<                     }
<                 }
<
<
<             }
<
<             AutoResizeColumnsInternal ();
<             OnColumnRemoved (e);
<             PrepareEditingRow (false, true);
<         }
<
<
diff -r
mono-2.0/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewRowCollection.cs
original/mono-2.0/mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewRowCollection.cs
243,244d242
<             int count = list.Count;
<
246,247d243
<
<             DataGridView.OnRowsRemoved (new
DataGridViewRowsRemovedEventArgs (0, count));
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20081113/93d6a1ff/attachment-0001.html 


More information about the Mono-devel-list mailing list