C# WinForm:DataTable中數據復制粘貼操作的實現


1. 需要實現類似於Excel的功能,就是在任意位置選中鼠標起點和終點所連對角線所在的矩形,進行復制粘貼。

2. 要實現這個功能,首先需要獲取鼠標起點和終點點擊的位置。

3. 所以通過GridView(我用的是dev這款插件)的MouseDown和MouseUp事件來獲取這連個位置。MouseDown獲得鼠標左鍵按下時的所在的行列信息,MouseUp獲得鼠標左鍵抬起時的行列信息。然后簡單加減法計算一共選中的行數和列數。

有了這個思路代碼其實挺簡單的。今天沒時間了,明天貼代碼。

----------------------------------------------------------------------------------------------

粘貼部分的代碼

try
            {
                string pasteText = Clipboard.GetText().Replace("\r\n", "@").TrimEnd('@');//得到剪切板里的信息
                string[] Row = pasteText.Split('@');//分割的行數數組
                string[] col = Row[0].Replace("\t", "#").TrimEnd('#').Split('#');//列數數組
                //判斷是否存在非數字類型 
                Regex rgx = new Regex(@"^[-]?\d+[.]?\d*$");
                for (int t = 0; t < Row.Length; t++)
                {
                    col = Row[t].Replace("\t", "#").TrimEnd('#').Split('#');
                    for (int t1 = 0; t1 < col.Length; t1++)
                    {
                        //判斷字符串
                        if (!rgx.IsMatch(col[t1].ToString().Trim()))
                        {
                            XtraMessageBox.Show("粘貼的內容存在非數字類型!");
                            return;
                        }
                    }
                }

                int _iRowCount = gridRowHandleEnd - gridRowHandleStar + 1;//一共選擇的行數
                int _iColCount = gridColHandleEnd - gridColHandleStar + 1;//一共選擇的列數
                for (int r = 0; r < Row.Length; r++)
                {
                    //獲取每行中的內容
                    string rowMessage = Row[r].Replace("\t", "#").TrimEnd('#');
                    col = rowMessage.Split('#');
                    DataRow dr = dtInputSurveyData.Rows[gridRowHandleStar + r];//r是從0開始的,gridRowHandleStar+r的作用等同於gridRowHandleStar++
                    //判斷外部復制內容的列數GridView中選中內容的列數的大小,最終粘貼內容的列數應與小的相同;
                    if (_iColCount < col.Length)
                    {
                        for (int i = 0; i < _iColCount; i++)
                        {
                            dr[gridColHandleStar + i] = col[i];
                        }
                    }
                    else
                    {
                        for (int i = 0; i < col.Length; i++)
                        {
                            dr[gridColHandleStar + i] = col[i];
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, Properties.Resources.ErrorTitle);
            }

還需要在gridview的MouseDown和MouseUp事件中添加代碼來獲取鼠標按下和抬起時的位置(我這里用的是dev這款插件,所以我這里的gridview是gridcontrol 下的gridview)。

MouseDown:

try
            {
                //當鼠標按下時獲取當前點擊cell的位置信息
                GridHitInfo _gridHI = gvInputSurveyData.CalcHitInfo(e.Location);
                if (e.Button == MouseButtons.Left && _gridHI.RowHandle >= 0)
                {
                    gridColHandleStar = _gridHI.Column.ColumnHandle;
                    gridRowHandleStar = _gridHI.RowHandle;
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, Properties.Resources.ErrorTitle);
            }

MouseUp:

            GridHitInfo _gridHI = gvInputSurveyData.CalcHitInfo(e.Location);
            if (e.Button == MouseButtons.Left && _gridHI.RowHandle >= 0)
            {
                gridColHandleEnd = _gridHI.Column.ColumnHandle;
                gridRowHandleEnd = _gridHI.RowHandle;
            }

這里兩個Start 變量和兩個end變量都是全局的,用來保存鼠標起始和終止的行列信息。

感謝您的閱讀,不足之處請見諒。期待與您在評論區交流。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM