Copy and paste multiple cells into a DataGridView

I saw that Datagridview does not allow you to copy and paste text from multiple cells, is there a simple option to enable this or do I need to use a keystore and clipboard data store to include that functionality.

The user wants to copy 3 cells in a row and be able to paste their text into another row.

+5
source share
4 answers

Ok, I have a solution, but it has not been tested by inserting cells in several rows. This is a KeyDown event in a datagridview

 if (e.Control && e.KeyCode == Keys.C)
            {
                DataObject d = AccountGrid.GetClipboardContent();
                Clipboard.SetDataObject(d);
                e.Handled = true;
            }
            else if (e.Control && e.KeyCode == Keys.V)
            {
                string s = Clipboard.GetText();
                string[] lines = s.Split('\n');
                int row = AccountGrid.CurrentCell.RowIndex;
                int col = AccountGrid.CurrentCell.ColumnIndex;
                string[] cells = lines[0].Split('\t');
                int cellsSelected = cells.Length;
                for (int i = 0; i < cellsSelected; i++)
                {
                    AccountGrid[col, row].Value = cells[i];
                    col++;
                }
            }
+5
source
        string s = Clipboard.GetText();
        string[] lines = s.Split('\n');
        int row = dataGridView1.CurrentCell.RowIndex;
        int col = dataGridView1.CurrentCell.ColumnIndex;
        foreach (string line in lines)
        {
            string[] cells = line.Split('\t');
            int cellsSelected = cells.Length;
            if (row < dataGridView1.Rows.Count)
            {
                for (int i = 0; i < cellsSelected; i++)
                {
                    if (col + i < dataGridView1.Columns.Count)
                        dataGridView1[col + i, row].Value = cells[i];
                    else
                        break;
                }
                row++;
            }
            else
            {
                break;
            }
        }
+1
source
if (e.Control && e.KeyCode == Keys.V)
{

string CopiedContent = Clipboard.GetText();
string[] Lines = CopiedContent.Split('\n');
int StartingRow = dataGridView1.CurrentCell.RowIndex;
int StartingColumn = dataGridView1.CurrentCell.ColumnIndex;
foreach (var line in Lines)
{
    if (StartingRow <= (dataGridView1.Rows.Count - 1))
    {
       string[] cells = line.Split('\t');
       int ColumnIndex = StartingColumn;
       for (int i = 0; i < cells.Length && ColumnIndex <= (dataGridView1.Columns.Count - 1); i++)
       {
          dataGridView1[ColumnIndex++, StartingRow].Value = cells[i];
       }
       StartingRow++;
   }
}}
0

// overview of data in adi metroGrid1 website dedicated to the website dedicated to the bulunan website // tabular in alt-jazzy word the word "document of kullan bilirsiniz"

private void metroGrid1_CellMouseClick (object sender, DataGridViewCellMouseEventArgs e) {

        if (e.Button == MouseButtons.Right)
        {
            ContextMenu m = new ContextMenu();
            m.MenuItems.Add("Kopyala", new EventHandler(kopyala_Click));
            m.MenuItems.Add("Yapıstır", new EventHandler(Yapıstır_Click));


            int currentMouseOverRow = metroGrid1.HitTest(e.X, e.Y).RowIndex;

            if (currentMouseOverRow >= 0)
            {
                m.MenuItems.Add(new MenuItem(string.Format("Do something to row {0}", currentMouseOverRow.ToString())));
            }

            m.Show(metroGrid1, new Point(e.X, e.Y));

        }
    }
    private void Yapıstır_Click(object sender, EventArgs e)
    {

        string s = Clipboard.GetText();
        string[] lines = s.Split('\r','\n','\t');
        int row = metroGrid1.CurrentCell.RowIndex;
        int col = metroGrid1.CurrentCell.ColumnIndex;


            int cellsSelected = lines.Length;
            if (row < metroGrid1.Rows.Count)
            {
            for (int i = 0; i < cellsSelected; i++)
            {
                if (col + i < metroGrid1.Columns.Count)
                {
                    if (lines[i] != "")
                    {
                        metroGrid1[col + i, row].Value = lines[i];
                    }
                    else
                    {
                        col -= 1;
                    }
                }
                else
                {break; }

                }
                row++;
            }


    }
0
source

All Articles