tags:

views:

919

answers:

3

Hey Guys! I m working on C# windows application. And I have to copy the whole row of a DataGridView and paste it into a new row. How I can acchieve this? I am using framework 3.5

Can you please give me atleast idea or code would be best that how i can acieve this?

Thank you!

+1  A: 

DataGridViewRow class has a .Clone method which will clone the current row that it holds.

Have a look here for more info

Tony
I checked it. But after copying data when i paste it into new rowof a gridview. It doesn't paste the data of the previous row while when i do paste in a single textbox..it occurs.I just copy paste msdn code function after the function of DataGridView.
Programmer
A: 

I have found a post that contains code to paste values from clipboard into DataGridView.

I was googling how to paste to DataGridView in C# from clipboard, an info, copied from Excel, and didn't find full answer. Collected couple of threads from forums and came up with this answer, hope it will make someones life easier. You dont have to understand the code just copy and paste

Below is a bit modified version. Beyond small refactoring I forbid paste into ReadOnly cells.

Usage example:

private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
{
    ClipboardUtils.OnDataGridViewPaste(sender, e);
}

Code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Commons
{
    public class ClipboardUtils
    {
        public static void OnDataGridViewPaste(object grid, KeyEventArgs e)
        {
            if ((e.Shift && e.KeyCode == Keys.Insert) || (e.Control && e.KeyCode == Keys.V))
            {
                PasteTSV((DataGridView)grid);
            }
        }

        public static void PasteTSV(DataGridView grid)
        {
            char[] rowSplitter = { '\r', '\n' };
            char[] columnSplitter = { '\t' };

            // Get the text from clipboard
            IDataObject dataInClipboard = Clipboard.GetDataObject();
            string stringInClipboard = (string)dataInClipboard.GetData(DataFormats.Text);

            // Split it into lines
            string[] rowsInClipboard = stringInClipboard.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries);

            // Get the row and column of selected cell in grid
            int r = grid.SelectedCells[0].RowIndex;
            int c = grid.SelectedCells[0].ColumnIndex;

            // Add rows into grid to fit clipboard lines
            if (grid.Rows.Count < (r + rowsInClipboard.Length))
            {
                grid.Rows.Add(r + rowsInClipboard.Length - grid.Rows.Count);
            }

            // Loop through the lines, split them into cells and place the values in the corresponding cell.
            for (int iRow = 0; iRow < rowsInClipboard.Length; iRow++)
            {
                // Split row into cell values
                string[] valuesInRow = rowsInClipboard[iRow].Split(columnSplitter);

                // Cycle through cell values
                for (int iCol = 0; iCol < valuesInRow.Length; iCol++)
                {

                    // Assign cell value, only if it within columns of the grid
                    if (grid.ColumnCount - 1 >= c + iCol)
                    {
                        DataGridViewCell cell = grid.Rows[r + iRow].Cells[c + iCol];

                        if (!cell.ReadOnly)
                        {
                            cell.Value = valuesInRow[iCol];
                        }
                    }
                }
            }
        }
    }
}
alex2k8
A: 

Can anyone please tell me how to do the same thing in Web application, because there is no key event for datagrid ?

anil
@anil: Welcome to StackOverflow! StackOverflow is a question/answer based site, so you should ask this as a separate question.
Zach Johnson