2016-10-29 48 views
0

我用下面的代碼與客戶對象填充gridview的,但我得到的錯誤:操作是無效的,因爲它會導致重入調用SetCurrentCellAddressCore功能gridview的錯誤:它會導致重入調用SetCurrentCellAddressCore功能

List<Customer> customers=new List<Customer>(); 
     customers.Add(customer); 
     DataTable dt = new DataTable(); 
     DataColumn col0= new DataColumn("Customer Id",typeof(int)); 
     DataColumn col1 = new DataColumn("Name", typeof(string)); 
     DataColumn col2 = new DataColumn("Address", typeof(string)); 
     DataColumn col3 = new DataColumn("City", typeof(string)); 
     DataColumn col4 = new DataColumn("State", typeof(string)); 
     DataColumn col5 = new DataColumn("ZipCode", typeof(string)); 
     dt.Columns.Add(col0); 
     dt.Columns.Add(col1); 
     dt.Columns.Add(col2); 
     dt.Columns.Add(col3); 
     dt.Columns.Add(col4); 
     dt.Columns.Add(col5); 
     int i = 0; 

     foreach (Customer item in customers) 
     { 
      DataRow drow = dt.NewRow(); 
      dt.Rows.Add(drow); 
      dt.Rows[i][col0] = Convert.ToInt32(item.CustomerID); 
      dt.Rows[i][col1] = item.Name.ToString(); 

      dt.Rows[i][col2] = item.Address.ToString(); 
      dt.Rows[i][col3] = item.City.ToString(); 
      dt.Rows[i][col4] = item.State.ToString(); 
      dt.Rows[i][col5] = item.ZipCode.ToString(); 
      i++; 
     } 

     grvCustomer.Visible = true; 

     grvCustomer.DataSource = dt; 

回答

0

Gridview通常會引發異常,以防止發生無限循環。 這樣做的原因通常是下列之一:

  • 改變有源單元,而在當前活動的細胞

  • 開始,結束或取消編輯模式而細胞中被執行的操作編輯是 已經正在進行

  • 導致活動單元格中的任何其它操作而改變 而在DataGridView仍然使用它

查看您的CellValueChanged事件處理程序,並確保您沒有在處理程序中執行上述任何操作。

快速解決方法可以使用BeginInvoke。 BeginInvoke是一個異步調用,所以gridview更改/編輯事件立即返回,之後執行該方法,那時gridview不再使用當前活動的單元格。

this.BeginInvoke(new MethodInvoker(() => 
     { 
      //do something 
     })); 
相關問題