关于C# 将DataGridView数据拖动到picturebox代码示例

时间:2022-06-25 13:52:49
  
  
  
this .PictureBoxNodeBackImage.AllowDrop = true ; // 这个也要加上
  
  
  
// 最后我们判断完成后怎样向PictureBox中添加数据,并从datagridview中删除选中数据所在的行,我们在PictureBox的DragDrop事件中执行操作
private void PictureBoxNodeBackImage_DragDrop( object sender, DragEventArgs e)
{
Point pos = new Point(e.X, e.Y);   //拖動數據后 所記錄的坐標
  pos = this.PictureBoxNodeBackImage.PointToClient(pos);

int index = - 1 ;
if (e.Data.GetDataPresent( typeof ( int )))
{
index
= ( int )e.Data.GetData( typeof ( int ));
}
if (index > - 1 )
MessageBox.Show(DataGridViewleizhi.Rows[index].Cells[
" Id " ].Value.ToString());
}
  
  
  
// 再次确定当数据拖动到PictureBox上方时,判断数据格式以及目标对象,和拖动方式.我们在PictureBox的DragEnter 事件中进行判断
private void PictureBoxNodeBackImage_DragEnter( object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent( typeof ( int )))
{
e.Effect
= DragDropEffects.Copy;
}
else
{
e.Effect
= DragDropEffects.None;
}
}
  
  
  
private void DataGridViewleizhi_CellMouseDown( object sender, DataGridViewCellMouseEventArgs e)
{
DataGridViewleizhi.DoDragDrop(e.RowIndex, DragDropEffects.Copy);
}