2015-07-10 27 views
1

在System.Windows.Forms.dll中發生類型'System.InvalidOperationException'的異常,但未在用戶代碼中處理 附加信息:行不能以編程方式刪除,除非DataGridView是數據綁定到IBindingList的支持更改通知並允許刪除Winforms datagridview:拖放導致錯誤

這是我的我的數據綁定到datagridview的:

IEnumerable<myTable> query = from p in db.myTables select p; 
     testList = query.ToList(); 
     dataGridView1.DataSource = testList; 

這是我用拖&降行:

 private void dataGridView1_MouseClick(object sender, MouseEventArgs e) 
    { 
     if (dataGridView1.SelectedRows.Count == 1) 
     { 
      if (e.Button == MouseButtons.Left) 
      { 
       rw = dataGridView1.SelectedRows[0]; 
       rowIndexFromMouseDown = dataGridView1.SelectedRows[0].Index; 
       dataGridView1.DoDragDrop(rw, DragDropEffects.Move); 
      } 
     } 
    }   
    private void dataGridView1_DragEnter(object sender, DragEventArgs e) 
    { 
     if (dataGridView1.SelectedRows.Count > 0) 
     { 
      e.Effect = DragDropEffects.Move; 
     } 
    } 
    private void dataGridView1_DragDrop(object sender, DragEventArgs e) 
    { 

     int rowIndexOfItemUnderMouseToDrop; 
     Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y)); 
     rowIndexOfItemUnderMouseToDrop = dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex; 

     if (e.Effect == DragDropEffects.Move) 
     { 
      dataGridView1.Rows.RemoveAt(rowIndexFromMouseDown); 
      dataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rw); 
     } 
    } 

每當我試圖拖動&下降時,拖動確定,但下降導致我上面提到的錯誤,如果可能的話我不想使用的BindingList,因爲如果我使用它,我將不得不做出很多的變化。 如果你能幫助我,我會很大膽。

+1

如果要雙向綁定,則需要使用綁定列表。 – SpaceSteak

+0

我不知道該怎麼做。 這db.EFESRDP0s選擇p不工作 '的IEnumerable 查詢=從磷; testList = query.ToList(); VAR列表=新的BindingList (testList); dataGridView1.DataSource =名單;' – BarisY

+0

不受限制,只要我知道,但你可以嘗試做一些研究一種替代。 – SpaceSteak

回答

0

我解決了這一問題。我對鼠標事件進行了更改

private void dataGridView1_MouseMove(object sender, MouseEventArgs e) 
    { 
     if ((e.Button & MouseButtons.Left) == MouseButtons.Left) 
     {     
      if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y)) 
      {          
       DragDropEffects dropEffect = dataGridView1.DoDragDrop(dataGridView1.Rows[rowIndexFromMouseDown], DragDropEffects.Copy); 
      } 
     } 
    } 
    private void dataGridView1_MouseDown(object sender, MouseEventArgs e) 
    { 
     // Get the index of the item the mouse is below. 
     rowIndexFromMouseDown = dataGridView1.HitTest(e.X, e.Y).RowIndex; 
     if (rowIndexFromMouseDown != -1) 
     {        
      Size dragSize = SystemInformation.DragSize; 
      dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width/2), e.Y - (dragSize.Height/2)), dragSize); 
     } 
     else    
      dragBoxFromMouseDown = Rectangle.Empty; 
    } 
    private void dataGridView1_DragOver(object sender, DragEventArgs e) 
    { 
     e.Effect = DragDropEffects.Copy; 
    } 
    private void dataGridView1_DragDrop(object sender, DragEventArgs e) 
    { 
     try 
     { 
      if (e.Data.GetDataPresent(typeof(DataGridViewRow))) 
      { 
       Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));     
       if (e.Effect == DragDropEffects.Copy) 
       { 
        DataGridViewRow Row = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow)); 
        dataGridView1.Rows.Add(Row.Cells[0].Value, Row.Cells[1].Value, Row.Cells[2].Value); 
       } 
      } 
      else 
      { 
       //Reflect the exception to screen 
       MessageBox.Show("Geen data! #01", "Error"); 
      } 
     } 
     catch (Exception msg) 
     { 
      MessageBox.Show(msg.Message, "Error"); 
     } 
    }