更新“索引” datagridview中的列 [英] Updating "index" column in datagridview

查看:168
本文介绍了更新“索引” datagridview中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试以编程方式更新dataGridView1表中的数据。所以,但是按下按钮我会添加另一行数据并且它具有顺序索引号(例如1,2,3,4 ......等)。



我还有一个删除行按钮,它将删除所选行 - 我想要实现的是更新所有行的索引值,所以当我删除让我们说第3行它仍将计数1,2,3,4而不是1,2,4,5。有任何想法吗? 索引是行号,因此必须是顺序的。





谢谢。



我尝试过:



Hi,

I'm trying to programmatically update my data in a dataGridView1 table. So, but pressing a button I add another row of data and it has an sequential index number (eg 1, 2, 3, 4... etc).

I have also a delete row button which will delete selected rows - what I'm trying to achieve is to update the index value for all the rows so when I delete lets say row 3 it will still count 1, 2, 3, 4 and not 1,2,4,5. Any ideas? The "index" is the line number, hence has to be sequential.


Thanks.

What I have tried:

<pre lang="c#">
        private void buttonDeleteRow_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in this.dataGridView1.SelectedRows)
            {
                if (!row.IsNewRow)
                { this.dataGridView1.Rows.RemoveAt(row.Index); }
            }
        }

推荐答案

好的,我找到了解决办法!



Ok, I found the solution!

foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (!row.IsNewRow)
                { row.Cells["Item"].Value = row.Index+1; }
            }


这篇关于更新“索引” datagridview中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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