国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
C#如何實(shí)現(xiàn)DataGridView到DataGridView的拖拽

今天工作中遇到一個(gè)問題,需要將一個(gè)DataGridView中的某一行拖拽到另一個(gè)DataGridView中,在網(wǎng)上搜了一遍,大多是從DataGridView拖拽到TextBox等控件,沒有拖拽到
DataGridView中的。拖拽到TextBox很容易,但拖拽到DataGridView就有一個(gè)問題:如何決定拖拽到DataGridView中的哪一個(gè)Cell?
為此研究了兩個(gè)小時(shí),終于找到了答案。
例如要實(shí)現(xiàn)從gridSource到gridTarget的拖拽,需要一個(gè)設(shè)置和三個(gè)事件:
1、設(shè)置gridTarget的屬性AllowDrop為True
2、實(shí)現(xiàn)gridSource的MouseDown事件,在這里進(jìn)行要拖拽的Cell內(nèi)容的保存,保存到剪貼板。
3、實(shí)現(xiàn)gridTarget的DragDrop和DragEnter事件,DragDrop事件中的一個(gè)難點(diǎn)就是決定拖拽到哪一個(gè)Cell

代碼如下:

gridSource的MouseDown事件:

Code
private void gridSource_MouseDown(object sender, MouseEventArgs e)
{
     if (e.Button == MouseButtons.Left)
     {
          DataGridView.HitTestInfo info = this.gridSource.HitTest(e.X, e.Y);
          if (info.RowIndex >= 0)
          {
              if (info.RowIndex >= 0 && info.ColumnIndex >= 0)
              {
                  string text = (String)this.gridSource.Rows[info.RowIndex].Cells[info.ColumnIndex].Value;
                   if (text != null)
                    {
                        this.gridSource.DoDragDrop(text, DragDropEffects.Copy);
                     }
               }
           }
       }
 }

 

gridTarget的DragDrop事件:

Code
private void gridTarget_DragDrop(object sender, DragEventArgs e)
{
       //得到要拖拽到的位置
     Point p = this.gridTarget.PointToClient(new Point(e.X, e.Y));
      DataGridView.HitTestInfo hit = this.gridTarget.HitTest(p.X, p.Y);
      if (hit.Type == DataGridViewHitTestType.Cell)
      {
            DataGridViewCell clickedCell = this.gridTarget.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
            clickedCell.Value = (System.String)e.Data.GetData(typeof(System.String));
       //如果只想允許拖拽到某一個(gè)特定列,比如Target Field Expression,則先要判斷列是否為Target Field Expression,如下:
             
//if (0 == string.Compare(clickedCell.OwningColumn.Name, "Target Field Expression"))
             
//{
             
//    clickedCell.Value = (System.String)e.Data.GetData(typeof(System.String));
             
//}
       }
}

 

gridTarget的DragEnter事件:

Code
private void gridTarget_DragEnter(object sender, DragEventArgs e)
{
     e.Effect = DragDropEffects.Copy;
}
分類: 02 C#/.NET
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
DataGridView DataGridViewCheckBoxColumn編輯時(shí)實(shí)時(shí)觸發(fā)事件
C# DataGridView中實(shí)現(xiàn)類似ComboBox可編輯的下拉框功能 - 清凈的雨天...
(轉(zhuǎn))使用DataGridView控件常見問題解答 - 邊寫邊唱 - 博客園
DataGridView自定義行樣式和行標(biāo)題
C# DataGridViewButtonColumn的使用---動(dòng)態(tài)改變按鈕的文本
Windows Forms DataGridView 中合并單元格(轉(zhuǎn))
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服