Click here to Skip to main content
15,914,162 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have datagridview named Dgv_Invoice connected with database .when i call data into 7th column in the first row i want to copy this value into the second row cell 7 from the above cell .and the third row cell alse from the above row cell.
but it works for all datagridview>
i want only to copy the previous cell the current cell

What I have tried:

private void Dgv_Invoice_KeyDown(object sender, KeyEventArgs e)
{
if (Dgv_Invoice.CurrentRow.Cells[7].Selected == true )
{
if (e.KeyCode == Keys.Enter)
{
if(Dgv_Invoice.Rows.Count>1)
{
for (int o = 0; o < Dgv_Invoice.Rows.Count - 2; o++)
{
Dgv_Invoice.Rows[o+1].Cells[7].Value = Dgv_Invoice.Rows[o].Cells[7].Value;

}
}
}

}
}
Posted
Updated 5-Nov-18 22:26pm
Comments
Richard MacCutchan 5-Nov-18 15:32pm    
Don't use a loop. Just use a single copy statement.

1 solution

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:

C#
private void Dgv_Invoice_KeyDown(object sender, KeyEventArgs e)
        {
            //The index of column involved in swap business
            int col_index = 6;
            //Total rows
            var rows = Dgv_Invoice.Rows;
            //The current row
            var current_row = Dgv_Invoice.CurrentRow;
            //The number of rows involved in swap business 
            int row_in_swap = 3;

            //Applicable rule definition
            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
 
Share this answer
 
v3
Comments
Abuamer 6-Nov-18 7:54am    
Yes you understand me will.so please give me alive example.i will be graceful for you.thank u for your interest
Abuamer 6-Nov-18 15:05pm    
thank you Aydin Homay . realy Helpful for me.iam graceful for you.thank u my friend

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