复制行更改一个单元格 [英] Copy Row change one cell

查看:63
本文介绍了复制行更改一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

我有一点问题。

我有一个包含2个datagridviews的表单。我选择要复制到新网格的行。



问题就出现了。我也有一个datetimepicker。在grid1中,我有一个带日期的列。但是,当我通过复制的文件时,我喜欢将该日期更改为datetimepickers。

Hi.
I have a little problem.
I have a form with 2 datagridviews. I select rows to copy over to the new grid.

Here comes the problem. I also have a datetimepicker. In grid1 I have a column with a date. But when I past the copied files I like it to change that date to datetimepickers.

try
{
    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
    {      ((DataTable)dataGridView2.DataSource).ImportRow(((DataRowView)row.DataBoundItem).Row);
    }
    foreach (DataGridViewRow row in dataGridView2.Rows)
    {
        row.Cells[2].Value = dtp_Datum.Value.ToString();
    }
}



此代码更改每一行。如果我复制2行然后更改日期,则网格2中的所有行都将获得最新更改日期。


This code change every row. If I copy 2 rows and then change date, all rows in grid 2 get the latest changed date.

推荐答案

您只需要修改添加的行。不幸的是,ImportRow不会返回新创建的行,但是根据手册,所有新行都会添加到数据表的末尾,因此您可以通过索引访问它们:



You need to only modify the added rows. Unfortunately, ImportRow does not return the newly created row, but according to the manual all new rows are added to the end of the datatable, so you can access them by index:

int numberOfRowsToCopy = dataGridView1.SelectedRows.Count;
int numberOfExistingRows = dataGridView2.Rows.Count;

foreach(DataGridViewRow row in dataGridView1.SelectedRows)
{
    ((DataTable)dataGridView2.DataSource).ImportRow(((DataRowView) row.DataBoundItem).Row);
}

for(int i = 0; i < numberOfRowsToCopy; ++i)
{
    dataGridView2.Rows[numberOfExistingRows + i].Cells[2].Value = dtp_Datum.Value.ToString();
}


这篇关于复制行更改一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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