2014-10-23 9 views
5

मेरे पास एक ऐसा फ़ंक्शन है जिसे सेल_एंडिट से कहा जाता है। यह एक DataGridView के अंदर एक dataGridViewRow ले जाता है: पंक्ति table.Rows.Insert परसेट करने के लिए पुनर्वित्त कॉल से कैसे बचें CurrentCellAddressCore?

private void moveRowTo(DataGridView table, int oldIndex, int newIndex) 
{ 
    if (newIndex < oldIndex) 
    { 
     oldIndex += 1; 
    } 
    else if (newIndex == oldIndex) 
    { 
     return; 
    } 
    table.Rows.Insert(newIndex, 1); 
    DataGridViewRow row = table.Rows[newIndex]; 
    DataGridViewCell cell0 = table.Rows[oldIndex].Cells[0]; 
    DataGridViewCell cell1 = table.Rows[oldIndex].Cells[1]; 
    row.Cells[0].Value = cell0.Value; 
    row.Cells[1].Value = cell1.Value; 
    table.Rows[oldIndex].Visible = false; 
    table.Rows.RemoveAt(oldIndex); 
    table.Rows[oldIndex].Selected = false; 
    table.Rows[newIndex].Selected = true; 
} 

(newIndex, 1) मैं प्राप्त निम्न त्रुटि:

Unhandled exception of type "System.InvalidOperationException" in System.Windows.Forms.dll

Additional data: Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.

यह तब होता है जब मैं करने की प्रक्रिया में एक और सेल क्लिक करें वर्तमान सेल संपादन। मैं इस तरह की त्रुटि से कैसे बचूं और मेरी पंक्ति सही तरीके से डाली गई है?

उत्तर

15

यह त्रुटि द्वारा

Any operation that results in the active cell being changed while the DataGridView is still using it

स्वीकार किए जाते हैं जवाब in this post के रूप में होता है।

फिक्स (मैंने सत्यापित किया है) पर कॉल करने के लिए BeginInvoke का उपयोग करें।

private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
{ 
    this.BeginInvoke(new MethodInvoker(() => 
     { 
      moveRowTo(dataGridView2, 0, 1); 
     })); 
} 

BeginInvoke, एक अतुल्यकालिक कॉल है, इसलिए dataGridView2_CellEndEdit रिटर्न तुरंत, और moveRowTo विधि नहीं रह गया है वर्तमान में सक्रिय सेल उपयोग कर रहा है, उसके बाद निष्पादित किया जाता है उस समय dataGridView2 पर।

-1
if (
    (datagridview.SelectedCells[0].RowIndex != datagridview.CurrentCell.RowIndex) || 
    (datagridview.SelectedCells[0].ColumnIndex!= datagridview.CurrentCell.ColumnIndex) 
    ) { return; } 
+1

हालांकि यह कोड प्रश्न का उत्तर दे सकता है, इस समस्या का जवाब दे सकता है कि यह कैसे हल करता है और/या समस्या हल करने के बारे में अतिरिक्त संदर्भ प्रदान करने से उत्तर के दीर्घकालिक मूल्य में सुधार होगा। –

संबंधित मुद्दे