可能这个问题对你来说并不重要,但我在这方面遇到了问题。我有一个DataGridView,我在其中添加行
If Me.Dgv.Rows.Count = 0 Then
SlNo = 1
Else
SlNo = Me.Dgv.Rows.Count + 1
End If
Me.Dgv.Rows.Add(SlNo, ItemId, ItemName, Qty, ItemRate, Amt)
并且行显示为......
-----------------------------------------------------------------
| Sl No | Item Id | Item Name | Qty | Rate | Amount |
-----------------------------------------------------------------
| 1 | 6 | Item6 | 10 | 100.00| 1000.00|
-----------------------------------------------------------------
| 2 | 8 | Item8 | 10 | 200.00| 2000.00|
-----------------------------------------------------------------
| 3 | 2 | Item2 | 10 | 300.00| 3000.00|
-----------------------------------------------------------------
| 4 | 16 | Item16 | 10 | 400.00| 4000.00|
-----------------------------------------------------------------
现在我要清除Row (Sl No. - 2)
,但我想保留下面的序列号。
-----------------------------------------------------------------
| Sl No | Item Id | Item Name | Qty | Rate | Amount |
-----------------------------------------------------------------
| 1 | 6 | Item6 | 10 | 100.00| 1000.00|
-----------------------------------------------------------------
| 2 | 2 | Item2 | 10 | 300.00| 3000.00|
-----------------------------------------------------------------
| 3 | 16 | Item16 | 10 | 400.00| 4000.00|
-----------------------------------------------------------------
或者我想清除Row (Sl No. - 1)
并希望保留序列号,如
-----------------------------------------------------------------
| Sl No | Item Id | Item Name | Qty | Rate | Amount |
-----------------------------------------------------------------
| 1 | 8 | Item8 | 10 | 200.00| 2000.00|
-----------------------------------------------------------------
| 2 | 2 | Item2 | 10 | 300.00| 3000.00|
-----------------------------------------------------------------
| 3 | 16 | Item16 | 10 | 400.00| 4000.00|
-----------------------------------------------------------------
这意味着我想删除/清除保留序列号的DataGridView
行。
我试过......
Private Sub DgvAutoSerialNumbering()
Dim SlNumber As Integer = 0
If Me.Dgv.Rows.Count = 0 Then
SlNumber = 1
ElseIf Me.Dgv.Rows.Count > 0 Then
For i As Integer = 0 To Me.Dgv.Rows.Count - 1
SlNumber = Me.Dgv.Rows.Count
Me.Dgv.CurrentRow.Cells(0).Value = SlNumber
Next
End If
End Sub
但未能获得输出。
我怎么能这样做?
提前致谢
答案 0 :(得分:3)
我找到了答案。对不起该帖子。
在DataGridView事件中搜索时,我发现了一个事件RowPrePaint
,只有三行代码解决了这个问题。
Private Sub Dgv_RowPrePaint(sender As Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles Dgv.RowPrePaint
If e.RowIndex >= 0 Then
Me.Dgv.Rows(e.RowIndex).Cells(0).Value = e.RowIndex + 1
End If
End Sub
感谢您查看我的帖子,也很遗憾因错过event
而浪费您的宝贵时间。
答案 1 :(得分:1)
DataGridView.RowPrePaint
必须重新粉刷时, datagridview
事件都会被提出
例如,当您拖动datagridview
另一个表单或MessageBox
时,就会发生这种情况
或当你最小化/隐藏form
并再次重新开启时......
在您的情况下,只有在更改了行数后才需要更新Serial No
。
'Method looping all rows and update value of `Sl No` column based on index of row
Private Sub Dgv_RowCountChanged()
For Each dgvr As DataGridViewRow in Me.Dgv.Rows
dgvr.Cells(0).Value = dgvr.Index + 1
Next
End Sub
方法Dgv_RowCountChanged
将在RowsAdded
和RowsRemoved
Private Sub Dgv_RowsAdded(object sender,
DataGridViewRowsAddedEventArgs e) Handles Dgv.RowsAdded
Me.DgvCountChanged()
End Sub
Private Sub Dgv_RowsRemoved(object sender,
DataGridViewRowsRemovedEventArgs e) Handles Dgv.RowsRemoved
Me.DgvCountChanged()
End Sub
答案 2 :(得分:1)
以下是如何创建每行的列索引自动生成序列号:
<Columns>
<asp:CommandField ButtonType="Image" CancelImageUrl="~/data/commands/cancel.gif"
DeleteImageUrl="~/data/commands/delete.gif" DeleteText="Verwijder" EditImageUrl="~/data/commands/edit.gif"
EditText="Bewerk" SelectImageUrl="~/data/commands/download.gif" SelectText="Download bestand"
ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" UpdateImageUrl="~/data/commands/save.gif"
UpdateText="Opslaan">
<HeaderStyle Width="10%" />
</asp:CommandField>
<asp:BoundField HeaderText="Nr." ReadOnly="true">
<ItemStyle HorizontalAlign="Right" Width="1%" />
</asp:BoundField>
</Columns>
然后在代码发送上:
protected void gridLidDocument_RowDataBound(object sender, GridViewRowEventArgs e){
if (e.Row.RowType.Equals(DataControlRowType.DataRow)){
e.Row.Cells[1].Text = "" + ((((GridView)sender).PageIndex * ((GridView)sender).PageSize) + (e.Row.RowIndex + 1));
}
}