DataGridView行添加事件 [英] DataGridView row added event

查看:279
本文介绍了DataGridView行添加事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DataGridView,并将列表绑定到数据源。



我已经有了正确的列,并且我准确地映射了字段。我想做的是处理 RowAdded RowDataBound (例如在aspx GridView中)事件。 / p>

我发现的唯一事件是行收藏,但是无论我有多少物品,它都只能发射4次我第一次绑定,其他两次绑定,



e.RowCount:1 e.RowIndex:0
e.RowCount:[n- 1] e.RowIndex:1 *其中n是我的商品数量



有没有一种方法可以处理每个商品?



编辑:不更改 DataSource = 绑定方法

解决方案

我也遇到了同样的问题。您可以从传递给Row Transactions事件处理程序的事件args中获得添加的行的索引和范围。使用此信息可以遍历添加的每行。 e.RowIndex e.RowCount 将使您确定添加的行。

 私有无效DataGridView1_Row☎联系人(object sender,System.Windows.Forms.DataGridViewRowwegoEventArgs e)
{
for (int index = e.RowIndex; index< = e.RowIndex + e.RowCount-1; index ++){
DataGridViewRow row = DataGridView1.Rows [index];

//在此处对添加的行执行操作
//如果希望传递单个行,则引发一个自定义RowAdded事件。
}
}

如果您想继承datagridview并自己制作在上面的循环内引发 RowAdded事件的网格。


I'm using a DataGridView and I bind a List to the DataSource.

I already have the right columns and I map exactly the fields. What I'm trying to do is handling a sort of RowAdded or RowDataBound (like in aspx GridView) event.

The only event that I found is RowsAdded but no matter how many items I have, it is fired only 4 times the first time i bound, and twice the other times, with values

e.RowCount:1 e.RowIndex:0 e.RowCount:[n-1] e.RowIndex:1 *where n is the number of my items

is there a way I can get to a handle for each item?

EDIT: without changing the DataSource = binding method

解决方案

I just ran into this same issue. You can get the index and range of the rows that were added from the event args passed to the RowsAdded event handler. Use this information to loop through each of the added rows. e.RowIndex and e.RowCount will let you determine the added rows.

private void DataGridView1_RowsAdded(object sender, System.Windows.Forms.DataGridViewRowsAddedEventArgs e)
{
    for (int index = e.RowIndex; index <= e.RowIndex + e.RowCount - 1; index++) {
        DataGridViewRow row = DataGridView1.Rows[index];

        // Do something with the added row here
        // Raise a custom RowAdded event if you want that passes individual rows.
    }
}

If you wanted you could inherit datagridview and make your own grid that throws a "RowAdded" event inside the loop above.

这篇关于DataGridView行添加事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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