datagridview粘貼功能實現

來源:互聯網
上載者:User

datagridview粘貼,,當複製總行列數超過datagridview行列數時,要能夠自動增加行與列:

見代碼:

 #region 粘貼        public int Paste(DataGridView dgv, string pasteText, int kind, bool b_cut)        {            try            {                if (kind == 0)                {                    pasteText = Clipboard.GetText();                }                if (string.IsNullOrEmpty(pasteText))                    return -1;                int rowNum = 0;                int columnNum = 0;                //獲得當前剪貼簿內容的行、列數                for (int i = 0; i < pasteText.Length; i++)                {                    if (pasteText.Substring(i, 1) == "\t")                    {                        columnNum++;                    }                    if (pasteText.Substring(i, 1) == "\n")                    {                        rowNum++;                    }                }                Object[,] data;                //粘貼板上的資料來自於EXCEL時,每行末都有\n,在DATAGRIDVIEW內複製時,最後一行末沒有\n                if (pasteText.Substring(pasteText.Length - 1, 1) == "\n")                {                    rowNum = rowNum - 1;                }                columnNum = columnNum / (rowNum + 1);                data = new object[rowNum + 1, columnNum + 1];                String rowStr;                //對數組賦值                for (int i = 0; i < (rowNum + 1); i++)                {                    for (int colIndex = 0; colIndex < (columnNum + 1); colIndex++)                    {                        rowStr = null;                        //一行中的最後一列                        if (colIndex == columnNum && pasteText.IndexOf("\r") != -1)                        {                            rowStr = pasteText.Substring(0, pasteText.IndexOf("\r"));                        }                        //最後一行的最後一列                        if (colIndex == columnNum && pasteText.IndexOf("\r") == -1)                        {                            rowStr = pasteText.Substring(0);                        }                        //其他行列                        if (colIndex != columnNum)                        {                            rowStr = pasteText.Substring(0, pasteText.IndexOf("\t"));                            pasteText = pasteText.Substring(pasteText.IndexOf("\t") + 1);                        }                        if (rowStr == string.Empty)                            rowStr = null;                        data[i, colIndex] = rowStr;                    }                    //截取下一行資料                    pasteText = pasteText.Substring(pasteText.IndexOf("\n") + 1);                }                /*檢測值是否是列頭*/                /*                //擷取當前選中儲存格所在的列序號                int columnindex = dgv.CurrentRow.Cells.IndexOf(dgv.CurrentCell);                //擷取擷取當前選中儲存格所在的行序號                int rowindex = dgv.CurrentRow.Index;*/                int columnindex = -1, rowindex = -1;                int columnindextmp = -1, rowindextmp = -1;                if (dgv.SelectedCells.Count != 0)                {                    columnindextmp = dgv.SelectedCells[0].ColumnIndex;                    rowindextmp = dgv.SelectedCells[0].RowIndex;                }                //取到最左上方的 儲存格編號                foreach (DataGridViewCell cell in dgv.SelectedCells)                {                    //dgv.Rows[cell.RowIndex].Selected = true;                    columnindex = cell.ColumnIndex;                    if (columnindex > columnindextmp)                    {                        //交換                        columnindex = columnindextmp;                    }                    else                        columnindextmp = columnindex;                    rowindex = cell.RowIndex;                    if (rowindex > rowindextmp)                    {                        rowindex = rowindextmp;                        rowindextmp = rowindex;                    }                    else                        rowindextmp = rowindex;                }                if (kind == -1)                {                    columnindex = 0;                    rowindex = 0;                }                //如果行數超過當前列表行數                if (rowindex + rowNum + 1 > dgv.RowCount)                {                    int mm = rowNum + rowindex + 1 - dgv.RowCount;                    for (int ii = 0; ii < mm+1; ii++)                    {                        dgv.DataBindings.Clear();                        DataRow row = row = ds.Tables[0].NewRow();                        ds.Tables[0].Rows.InsertAt(row, ii + rowindex + 1);                    }                }                //如果列數超過當前列表列數                if (columnindex + columnNum + 1 > dgv.ColumnCount)                {                    int mmm = columnNum + columnindex + 1 - dgv.ColumnCount;                    for (int iii= 0; iii < mmm; iii++)                    {                        dgv.DataBindings.Clear();                        DataGridViewTextBoxColumn colum = new DataGridViewTextBoxColumn();                      dgv.Columns.Insert(columnindex+1, colum);                    }                }                //增加超過的行列                for (int j = 0; j < (rowNum + 1); j++)                {                    for (int colIndex = 0; colIndex < (columnNum + 1); colIndex++)                    {                        if (colIndex + columnindex < dgv.Columns.Count)                        {                            if (dgv.Columns[colIndex + columnindex].CellType.Name == "DataGridViewTextBoxCell")                            {                                if (dgv.Rows[j + rowindex].Cells[colIndex + columnindex].ReadOnly == false)                                {                                    dgv.Rows[j + rowindex].Cells[colIndex + columnindex].Value = data[j, colIndex];                                    dgv.Rows[j + rowindex].Cells[colIndex + columnindex].Selected = true;                                }                            }                        }                    }                }                        //清空剪下板內容                if (b_cut)                    Clipboard.Clear();                return 1;            }            catch            {                return -1;            }        }        #endregion

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.