dataGridView 如何?行行的拖動,即行之間相互交換位置

來源:互聯網
上載者:User
private void dataGridView_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            if ((e.Clicks < 2) && (e.Button==MouseButtons.Left))
            {
                if ((e.ColumnIndex ==-1) && (e.RowIndex > -1))
                    dataGridView.DoDragDrop(dataGridView.Rows[e.RowIndex], DragDropEffects.Move);
            }
        }
滑鼠在儲存格裡移動時啟用拖放功能,我這裡判斷了如果是只有單擊才執行拖放,雙擊我要執行其他功能,而且只有點在每行的表頭那一格拖動才行,否則會影響編輯其他儲存格的值。如果希望點在任何一個儲存格都可以拖動,去掉判斷列序號的條件就行了。

        private void dataGridView_DragDrop(object sender, DragEventArgs e)
        {
            int idx = GetRowFromPoint(e.X, e.Y);

            if (idx < 0) return;

            if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
            {
                DataGridViewRow row = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));
dataGridView.Rows.Remove(row);
dataGridView.Rows.Insert(idx,row);
selectionIdx=idx;
            }
        }
拖動到目的地後的操作,GetRowFromPoint是根據滑鼠按鍵被釋放時的滑鼠位置計算行序號。

        private int GetRowFromPoint(int x, int y)
        {
            for (int i = 0; i < dataGridView.RowCount; i++)
            {
                Rectangle rec = dataGridView.GetRowDisplayRectangle(i, false);

                if (dataGridView.RectangleToScreen(rec).Contains(x, y))
                    return i;
            }

            return -1;
        }

到這裡準系統其實已經完成。只是有些細節需要調整。一般我們希望拖動完成後被選中的行是剛才拖動的那一行,但是DataGridView會自動選中頂替我們拖動的那一行位置的新行,也就是說,比如我們拖動第3行到其他位置,結束後,被選中的行仍然是新的第3行。在DragDrop方法裡設定也沒有用,DataGridView選中新行的行為是在DragDrop結束後。所以我們必須自己記錄選中行的行號。在類中添加一個欄位selectionIdx。

        private void dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex>=0)
                selectionIdx = e.RowIndex;
}
注意DragDrop方法最後,selectionIdx被賦值為拖動行的新行號。

        private void dataGridView_SelectionChanged(object sender, EventArgs e)
        {
            if ((dataGridView.Rows.Count > 0) && (dataGridView.SelectedRows.Count>0) && (dataGridView.SelectedRows[0].Index != selectionIdx))
            {
                if (dataGridView.Rows.Count <= selectionIdx)
                    selectionIdx = dataGridView.Rows.Count - 1;
                dataGridView.Rows[selectionIdx].Selected = true;
                dataGridView.CurrentCell = dataGridView.Rows[selectionIdx].Cells[0];
            }
        }

在這裡判斷,如果selectionIdx的值和當前選中的行號不同,則選中行號為selectionIdx的這一行。

        private void dataGridView_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }
這個方法設定在DataGridView裡拖動時,滑鼠不會顯示禁止拖放的游標。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.