C#DataGridView - 克隆行到“NewRow”用于编辑目的 [英] C# DataGridView - Clone Row to "NewRow" for editing purposes

查看:548
本文介绍了C#DataGridView - 克隆行到“NewRow”用于编辑目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我的项目的一部分是让用户可以选择将行克隆/复制到他们想要制作的底部newrow 1或2个单元格编辑。这就是我到目前为止完成这项任务的目的。



Hi everyone!

Part of my project is to give the user an option to clone/duplicate a row to the bottom "newrow" incase they want to make 1 or 2 cell edits. This is what I have so far in accomplishing this mission.

try
            {
                highlightBottomRow();
                setDuplicateRowValues();
                setNewRowCellValues();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }





setDuplicateRowValues应该将选定的行单元格复制到变量(每个单元格1个变量)。 setNewRowCellValues应该设置newrow(编辑行)中单元格的值,包含变量所包含的值。是否有更有效的方法(即使用克隆方法)?第一列是一个ID列,我们想要增加每个新的添加(无论它是一个重复的行还是一个全新的行,并保持第一列是只读的)。我有25列,希望它尽可能提高性能。



谢谢大家!



setDuplicateRowValues is supposed to copy the selected row cells to a variable (1 variable per cell). setNewRowCellValues is supposed to set the values of the cells in the newrow(editing row) the values of what the variables contain. Is there a more efficient way to do this (i.e. with the Clone method)? The first column is an ID column and we want to increment each new addition (no matter if it''s a duplicated row or a row created brand new and keep the first column read-only). I have 25 columns and want this to be as performance efficient as possible.

Thanks everyone!

推荐答案

DataRow有一个ItemArray属性,它将所有字段值表示为对象[]。



DataRow existingrow = //您现有的行;

var newrow = tbl.NewRow();

newrow.ItemArray = existingrow.ItemArray;



如果你没有配置DataTable的架构,那么只需增加ID。这为您提供了一些保护,以防止将来的列更改和数据类型一致性。应该更快 - 但我自己还没有基准测试。
DataRow''s have an ItemArray property that represent all the field values as an object[].

DataRow existingrow = //Your existing row;
var newrow = tbl.NewRow();
newrow.ItemArray = existingrow.ItemArray;

And then just increment the ID if you''re DataTable''s schema isn''t configured to do so. This gives you a bit of protection against future column changes and data type consistency. Should be faster too - haven''t benchmarked it myself yet though.


这篇关于C#DataGridView - 克隆行到“NewRow”用于编辑目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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