DataGridview删除行并维护项目的序列号 [英] DataGridview Remove Row and to Maintain the Serial No of Items
问题描述
问题出在
我正在Datagridview中添加行
以编程方式在Button上单击
作为
The Issue Is
I am adding Rows in a Datagridview
programmatically on Button Click
As
Dim s As Integer = Val(txtSlNo.Text)
Dim c As Integer
If s = 0 Then
c = dgvInvoice.Rows.Count
s = c + 1
dgvInvoice.Rows.Add()
Else
c = s - 1
End If
dgvInvoice.Rows(c).Cells(0).Value = s.ToString()
此代码段位于添加按钮单击上,这将添加行
并为每行分配序列号.
接下来,我有一个按钮Delete Row.在Delete按钮的单击事件中
正在删除如下所示的选定行
This Snippet is on add Button click which is adding Row
and assigning Serial No to each row.
Next I have a button Delete Row .On the click event of Delete Button
am removing the Selected Rows as Below
For Each row As DataGridViewRow In dgvInvoice.SelectedRows
Dim response As DialogResult = _
MessageBox.Show( _
"Are you sure you want to delete this row?", _
"Delete row?", _
MessageBoxButtons.YesNo, _
MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button2)
If (response = DialogResult.No) Then
Else
dgvInvoice.Rows.Remove(row)
End If
Next
现在的问题是,如果用户在DatagridView中添加了五行
并从Datagridview中删除了序列号为3或任何编号的行.
我希望序列号也应相应更改.
我想你理解我的问题.
如何做到这一点
请协助.
Now the Problem is If the user Added Five rows In DatagridView
and removed the Row having Serial no 3 or any from the Datagridview.
I want that Serial no should also change Accordingly.
I think you understanding my Question.
How to Acheive this
Please assist.
推荐答案
在dgvInvoice的 RowsRemoved 事件
内
inside dgvInvoice''s RowsRemoved event
For i=0 to dgvInvoice.Rows.count-1
dgvInvoice.Rows(i).Cells(0).Value = (i+1).ToString()
Next
祝您编程愉快!
:)
Happy coding!
:)
这篇关于DataGridview删除行并维护项目的序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!