Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

copy and paste function in winform datagridview

$
0
0

I am trying to develop a copy and paste function in winform datagridview.

there are some text columns and combobox columns in datagrid. and the combobox have key and value.

i am trying to copy some rows data and pasted into other rows,

for text column,my function can copy and get the value, but for combobox cells, when debug, the 

Clipboard.SetText(theCellsData); i can see the key in set in Clipboard, but when i use CTR+V and paste it in a notepad,

it still returns me the value.

here are my code:

private void Copy2ClipperBoard(DataGridView dataGrid)
        {
            if (dataGrid != null)
            {
                dataGrid.EndEdit();
                string theCellsData = GetSelectedCellsData(dataGrid);

                if (theCellsData != null)
                {
                        try
                        {
                            //Clipboard.SetDataObject(theCellsData, true);
                           Clipboard.SetText(theCellsData);
                        }
                        catch (Exception ex)
                        {
                            try
                            {
                                Clipboard.SetDataObject(theCellsData, false, 3, 300);
                            }
                            catch { ShellUtils.ShowError(ex.Message); }
                        }
                    }
                }
        }

        private string GetSelectedCellsData(DataGridView dataGrid)
        {
            DataGridViewSelectedCellCollection selection = dataGrid.SelectedCells;
            if (selection.Count == 0) return null;

            if (selection.Count == 1)
            {
                return GetDataFromSingleCell(dataGrid, selection[0], 0, 0);
            }
            else
            {
                int minColumnIndex, maxColumnIndex;
                Dictionary<int, Dictionary<int, DataGridViewCell>> selectedCells =
                    GetSelectedCells(dataGrid, out minColumnIndex, out maxColumnIndex);

                int[] selectedRowIndexes = new int[selectedCells.Count];
                selectedCells.Keys.CopyTo(selectedRowIndexes, 0);
                Array.Sort(selectedRowIndexes);

                string cellContent;
                StringBuilder sb = new StringBuilder();
                int rowOffset = 0, columnOffset;

                for (int i = selectedRowIndexes[0]; i <= selectedRowIndexes[selectedRowIndexes.Length - 1]; i++)
                {
                    if (selectedCells.ContainsKey(i))
                    {
                        for (int j = minColumnIndex; j <= maxColumnIndex; j++)
                        {
                            columnOffset = j - minColumnIndex;
                            if (selectedCells[i].ContainsKey(j))
                            {
                                cellContent = GetDataFromSingleCell(dataGrid, selectedCells[i][j], rowOffset, columnOffset);
                                sb.Append(cellContent);
                            }
                            if (j < maxColumnIndex) sb.Append('\t');
                        }
                    }
                    sb.Append("\r\n");
                    rowOffset++;
                }
                return sb.ToString();
            }
        }


        private string GetDataFromSingleCell(
            DataGridView dataGrid, DataGridViewCell theCell, int rowOffset, int columnOffset
            )
        {
            if (dataGrid == null || theCell.ColumnIndex == 0) return null;
            if (theCell.RowIndex == dataGrid.NewRowIndex) return String.Empty;

            string cellData = null;
            DataGridViewColumn column = dataGrid.Columns[theCell.ColumnIndex];
            if (column.GetType() == typeof(System.Windows.Forms.DataGridViewComboBoxColumn))
            {
                cellData = theCell.Value.ToString();
            }
            else
            {
                cellData = theCell.Value.ToString();
            }


            cellData = cellData.Replace("\\", "\\\\").Replace("\n", "\\n").Replace("\t", "\\t");
            return cellData;
        }

        private static Dictionary<int, Dictionary<int, DataGridViewCell>> GetSelectedCells(
            DataGridView dataGrid, out int minColumnIndex, out int maxColumnIndex
            )
        {
            minColumnIndex = 999;
            maxColumnIndex = -1;
            Dictionary<int, Dictionary<int, DataGridViewCell>> rowsData = new Dictionary<int, Dictionary<int, DataGridViewCell>>();

            foreach (DataGridViewCell theCell in dataGrid.SelectedCells)
            {
                if (theCell.ColumnIndex == 0 || theCell.ColumnIndex == 1) continue;

                int columnIndex = theCell.ColumnIndex;
                int rowIndex = theCell.RowIndex;
                if (minColumnIndex > columnIndex) minColumnIndex = columnIndex;
                if (maxColumnIndex < columnIndex) maxColumnIndex = columnIndex;

                Dictionary<int, DataGridViewCell> currentRow;
                if (rowsData.ContainsKey(rowIndex))
                {
                    currentRow = rowsData[rowIndex];
                }
                else
                {
                    currentRow = new Dictionary<int, DataGridViewCell>();
                    rowsData[rowIndex] = currentRow;
                }
                currentRow[columnIndex] = theCell;
            }
            return rowsData;
        }

how to copy combobox cells and get the real key, not text from grid?


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>