如何在datagridview C#中将前一个单元格值复制到当前单元格 [英] How can I copy the previous cell value to current cell in datagridview C#

查看:240
本文介绍了如何在datagridview C#中将前一个单元格值复制到当前单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Dgv_Invoice的datagridview与数据库连接。当我将数据调用到第一行的第7列时,我想将这个值从上面的单元格复制到第二行单元格7中。而第三行单元格也来自上面的行单元格。

但它适用于所有datagridview>

i只想复制前一个单元格当前单元格



我尝试过:



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;



}

}

}



}

}

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;

}
}
}

}
}

推荐答案

如果我理解你想要在第一行的第7列按Enter键然后你想要将当前单元格的值复制到下面的单元格,并且下一个单元格的值相同到下一个细胞。以一种简单的方式,您希望将第1行的第7列的值移动到第2行以下,将第2行的值移动到第3行。例如:如果您的datagridview的第7列是这样的:



第1行,第7列:A

第2行,第7列:B
第3行,第7列:C



在第一行第7列按Enter后,您希望获得以下输出:



第1行,第7列:A

第2行,第7列:A

第3行,第7列:B



如果是,请使用以下代码:



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)
        {
            //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;
        }





干杯,

AH



Cheers,
AH


这篇关于如何在datagridview C#中将前一个单元格值复制到当前单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆