c#winforms DataGridView-如何重复选定的一个或多个单元格的内容? [英] c# winforms DataGridView - how to repeat content of selected cell or cells ?

查看:53
本文介绍了c#winforms DataGridView-如何重复选定的一个或多个单元格的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c#winforms
如何复制(重复)selectedCellsValue-直到行末.
连续的单元格(即列)数量-有所不同(约30个).
Excel具有名为-autoFill-Copy的选项.

这是我的尝试:

c# winforms
How can I copy (repeat) selectedCellsValue - till end of row.
Number of cells (i.e. Columns) in a row - varies (about 30).
Excel have that option named - autoFill - Copy.

Here is my try:

int x = dgv.SelectedCells.Count;
foreach (DataGridViewCell c in dgv.CurrentRow.Cells)
{
    if (c.Selected == true)
    {
        string a = c.Value.ToString();
       x = x+1;
   dgvRasp.CurrentRow.Cells[x].Value = a;

   But it copies selectedeCells only once.

推荐答案

Copy current cell的内容(单击单元格箭头,导航到使用箭头键使其变为当前状态),可以按如下所示处理DataGridView KeyDown 事件,并且可以在按下Ctrl+Enter时将内容复制到当前行的最后一个单元格.
To Copy the contents of the current cell (click on a cell arrow navigate to a cell using arrow keys to make it a current) the KeyDown event of the DataGridView can be handled as shown below and the contents can be copied upto to the last cell in the current row when Ctrl+Enter is pressed.
dataGridView1.KeyDown += (sender, args) => {
    if (!args.Control || args.KeyCode != Keys.Enter)
        return;
    var currentVal = dataGridView1.CurrentCell.Value;
    for(int i=dataGridView1.CurrentCell.ColumnIndex; i<dataGridView1.Columns.Count; i++){
        dataGridView1.CurrentRow.Cells[i].Value=currentVal;
    }
};


这篇关于c#winforms DataGridView-如何重复选定的一个或多个单元格的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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