简体   繁体   English

如何从datagridview中删除行并重置特定列(vb.net 2005)

[英]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: 所以,你需要处理RowsRemovedRowsAdded的事件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.

相关问题 如何在VB.net的DataGridview中在最后位置添加行并从当前位置删除行 - How to Add Row at Last position and Remove Row from current position In DataGridview in VB.net VB.NET DataGridView-如果您知道第2列的行值,如何找出第1列的值 - VB.NET DataGridView - How to find out Column 1 value if you know row value from Column 2 如何将datagridview中的特定行添加到VB.net中的列表框 - How to add specific row from datagridview to listbox in VB.net 如何在DataGridView VB.net中四舍五入一列 - How to round a column in a DataGridView VB.net 如果行/列单元为空,则突出显示datagridview行(vb.net) - highlight datagridview row if row/column cell is empty (vb.net) Select ComboBox 中的特定行,并在 DataGridView VB.NET 中同一行的其他列中添加金额 - Select a specific row from ComboBox and add amount in other Column from same row in DataGridView VB.NET 如何使用VB.Net在datagridview中的特定列中设置单元格格式? - How to set cell formatting in particular column in datagridview using VB.Net? 将vb.net中具有特定列和行的csv文件导入到datagridview中 - import csv file into datagridview with specific column and row in vb.net 在Datagridview中设置Column BackColor以覆盖VB.net中的Row BackColor - Set Column BackColor in Datagridview to override Row BackColor in VB.net 如何从 VB.NET 中的 Session 中删除 gridview 中的选定行? - How to remove selected row in a gridview from a Session in VB.NET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM