Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
c# winforms
How can I copy (repeat) selectedCellsValue - till end of row.
Number of cells (i.e. Columns) in a row - varies (about 30).
Excel have that option named - autoFill - Copy.

Here is my try:

C#
int x = dgv.SelectedCells.Count;
foreach (DataGridViewCell c in dgv.CurrentRow.Cells)
{
    if (c.Selected == true)
    {
        string a = c.Value.ToString();
       x = x+1;
   dgvRasp.CurrentRow.Cells[x].Value = a;

   But it copies selectedeCells only once.
Posted
Updated 2-Jun-12 6:52am
v4
Comments
VJ Reddy 2-Jun-12 12:43pm    
Edit: pre tag for C# code added.

1 solution

To Copy the contents of the current cell (click on a cell arrow navigate to a cell using arrow keys to make it a current) the KeyDown event of the DataGridView can be handled as shown below and the contents can be copied upto to the last cell in the current row when Ctrl+Enter is pressed.
C#
dataGridView1.KeyDown += (sender, args) => {
    if (!args.Control || args.KeyCode != Keys.Enter)
        return;
    var currentVal = dataGridView1.CurrentCell.Value;
    for(int i=dataGridView1.CurrentCell.ColumnIndex; i<dataGridView1.Columns.Count; i++){
        dataGridView1.CurrentRow.Cells[i].Value=currentVal;
    }
};
 
Share this answer
 
Comments
Sunny_Kumar_ 2-Jun-12 16:27pm    
My 5+
VJ Reddy 2-Jun-12 19:50pm    
Thank you :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900