问题描述
我正在使用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:0e.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行添加事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!