Question

Is there any way I can make datagridview control so that user can copy and paste more than one cell from excel by using Ctrl+C and Ctrl+V ?

Was it helpful?

Solution

If you get simple Excel content by calling Clipboard.GetText() it will return a string.

In it the rows will be separated by Linefeeds (\r\n) and the fields by Tabs (\t).

Look at the code below, this will give you something to work with..:

    // Before you press the button copy some excel cells!
    private void button1_Click(object sender, EventArgs e)
    {
        dataGridView1.Rows.Clear();
        dataGridView1.Columns.Clear();

        dataGridView1.Columns.Add("one", "one");
        dataGridView1.Columns.Add("two", "two");
        dataGridView1.Columns.Add("three", "three");
        dataGridView1.Columns.Add("four", "four");
        dataGridView1.Columns.Add("five", "five");
        dataGridView1.Rows.Add(5);

        dataGridView1.Focus();
        dataGridView1.CurrentCell = dataGridView1[1, 1];
        string s = Clipboard.GetText();
        string[] lines = s.Replace("\n", "").Split('\r');
        string[] fields;
        int row = 0;
        int column = 0;
        foreach (string l in lines)
        { 
            fields = l.Split('\t');
            foreach (string f in fields)
                dataGridView1[column++, row].Value = f;
            row++;
            column = 0;
        }

    }

Pasting with Control-V is trickier, start with a button!

It would involve capturing the Control-V and processing it; this can be a bit hard to debug, though..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top