Click here to Skip to main content
15,881,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagridview with 5 columns.
For each new rowadded I want to copy the last rows' values into the new one except for one.
can anyone help me pls?

I'm a bit new to c# :/

I've been trying something like this

C#
foreach (DataGridViewColumn Column in dataGridView1.Columns)
           {
               foreach (DataGridViewCell cell in dataGridView1.Rows)
                {
    
                       if (cell.RowIndex != lastrowindex)
                       {
                           if (cell.FormattedValue == String.Empty)
                           {
                               listNames.Add(cell.Value.ToString());
                           }
                       }
                }
           }
           string strNames = null;
           foreach (string name in listNames)
               strNames += name + Environment.NewLine;
           MessageBox.Show("List of all rows\n\n" + strNames);


All help is very much appreciated!
Posted
Updated 10-Apr-14 6:15am
v2
Comments
Maciej Los 10-Apr-14 13:24pm    
WinForms, WebControls, WPF?
Dávid Szentgyörgyi 15-Apr-14 12:51pm    
Winforms

1 solution

C#
private void CopyRows(DataGridView DGVGrid, int SourceRowID, int DestinationRowID)

{

for (int j = 0; j < DGVGrid.Rows[SourceRowID].Cells.Count; j++)

DGVGrid.Rows[DestinationRowID].Cells[j].Value = DGVGrid.Rows[SourceRowID].Cells[j].Value;

}


// now call funcation

C#
private cpyButton_Click(object sender, EventArgs e)
{
int myRowIndex = myGrid.CurrentRow.Index;
for (int count = 0; count < myGrid.Rows.Count; count++)
{
CopyRows(myGrid, myRowIndex, myRowIndex + 1);
}
}



//Now call the function CopyRows(DataGridView1, 0, 1); in order to copy Row at position 0 to Row at position 1. In order to copy multiple rows, call the function in a loop.

//this is only to copy from any rows of the grid to the next row.
 
Share this answer
 

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