2011-12-06 16 views
8

DataGridView नियंत्रण में पंक्ति को हटाते समय मुझे यह त्रुटि क्यों मिल रही है? मैं इस मुद्दे को कैसे संबोधित कर सकता हूं?DataGridView नियंत्रण में पंक्ति को हटाते समय मुझे यह त्रुटि क्यों मिल रही है?

Rows cannot be programmatically removed unless the DataGridView is data-bound to an IBindingList that supports change notification and allows deletion. 

public partial class Form1 : Form 
    { 
     List<Person> person = new List<Person>(); 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     void Form1Load(object sender, EventArgs e) 
     { 
      person.Add(new Person("McDonalds", "Ronald")); 
      person.Add(new Person("Rogers", "Kenny"));   
      dataGridView1.DataSource = person; 
     } 

     void BtnDeleteClick(object sender, EventArgs e) 
     { 
      dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index); 
     } 
    } 

उत्तर

14

List<T>IBindingList को लागू नहीं करता है,

public class List<T> : IList<T>, ICollection<T>, 
    IEnumerable<T>, IList, ICollection, IEnumerable 

आप एक वर्ग को लागू करने वाली IBindingList

उपयोग एक BindingList<T> या बजाय DataTable

उपयोग करने की आवश्यकता
+0

सूची की तो बजाय, मैं इसे कुछ BindingList बनाने के लिए है? – yonan2236

+0

हां। यह काम करना चाहिए। –

+0

धन्यवाद और Google। बस इस त्रुटि में अभी भाग गया :) – Latheesan

2

आपको person सूची से एक तत्व निकालना होगा।

person.RemoveAt(0); 
0

मेरे समाधान:

void BtnDeleteClick(object sender, EventArgs e) 
{ 
    person.RemoveAt(dataGridView1.SelectedRows[0].Index); 
} 
संबंधित मुद्दे