[英]How to remove a row from datagridview and reset a particular column (vb.net 2005)
I am using VS 2005 edition. 我正在使用VS 2005版。 I am developing a winform desktop application. 我正在开发winform桌面应用程序。 Let's say I have a unbound datagridview, with table like below: 假设我有一个未绑定的datagridview,其表如下所示:
Original datagrid before delete: 删除之前的原始数据网格:
If I removed Stuff D (Item No 5), the "Item No" column supposed to reset itself accordingly. 如果我删除了东西D(项目编号5),则“项目编号”列应相应地进行自身重置。 The expected output should be: 预期输出应为:
After delete row: 删除行后:
The "Item No" column is not an autonumber, it's just the number I assigned incrementally as the user add in a new row(new Stuff). “项目编号”列不是自动编号,它只是我在用户添加到新行(新材料)中时递增分配的编号。 I tried using the following code in rowremoved event but failed to achieve the expected output. 我尝试在rowremoved事件中使用以下代码,但未能实现预期的输出。 Please help. 请帮忙。 Thanks. 谢谢。
If the goal is just showing the record index in cell, then you don't need to assign a value to cell and it's enough to set e.Value = e.RowIndex + 1
in CellFormatting
event. 如果目标只是在单元格中显示记录索引,则无需为单元格分配值,并且足以在CellFormatting
事件中设置e.Value = e.RowIndex + 1
。
But based on your comment it seems you need those cells have values as well. 但是根据您的评论,看来您也需要这些单元格也具有值。 So you need to handle RowsRemoved
and RowsAdded
event of DataGridView
and refresh the cell value with row number: 所以,你需要处理RowsRemoved
和RowsAdded
的事件DataGridView
和刷新行号为单元格的值:
Public Sub RefreshRowNumbers(g As DataGridView)
For Each r As DataGridViewRow In g.Rows
r.Cells(1).Value = r.Index + 1
Next
End Sub
Private Sub DataGridView1_RowsRemoved(sender As Object, _
e As DataGridViewRowsRemovedEventArgs) Handles DataGridView1.RowsRemoved
RefreshRowNumbers(DirectCast(sender, DataGridView))
End Sub
Private Sub DataGridView1_RowsAdded(sender As Object, _
e As DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
RefreshRowNumbers(DirectCast(sender, DataGridView))
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.