If I understood right you want to press Enter at the 7th column of the first row and then you want to copy the value of the current cell right to the below cell, and the same for the cell next to the next cell. In a simple way, you want to shift values of the 7th column at 1st row into the below 2nd row and value of the 2nd row into the 3rd row. For example: if the 7th column of your datagridview is like:
Row1, Column7: A
Row2, Column7: B
Row3, Column7: C
You would like to have the below output after pressing Enter at the first-row column 7:
Row1, Column7: A
Row2, Column7: A
Row3, Column7: B
If so, then use the following code:
private void Dgv_Invoice_KeyDown(object sender, KeyEventArgs e)
{
int col_index = 6;
var rows = Dgv_Invoice.Rows;
var current_row = Dgv_Invoice.CurrentRow;
int row_in_swap = 3;
bool applicable = rows.Count > 1 && (rows.Count - current_row.Index) > row_in_swap;
applicable &= e.KeyCode == Keys.Enter;
applicable &= current_row.Cells[col_index].Selected == true;
if (applicable)
SwapCellValues(rows, current_row, col_index);
}
private void SwapCellValues(DataGridViewRowCollection rows, DataGridViewRow current_row, int col_index)
{
var temp = rows[current_row.Index + 1].Cells[col_index].Value;
rows[current_row.Index + 1].Cells[col_index].Value = current_row.Cells[col_index].Value;
rows[current_row.Index + 2].Cells[col_index].Value = temp;
}
Cheers,
AH