问题描述
我想搜索抛出DataGridView,如果我发现记录中的Cell以51开头,如果带有52的星星做了什么,如果以53开头的东西做什么
代码有效,如果记录是51 52 53这样的订单但如果是51 51 52 52 53则选择第一条记录并停止
我想要它选择开头的第一条记录(51),(52),(53)是否有任何
这是代码
I want to Search throw DataGridView if I find the Cell in Record starts with "51" do something if Stars with "52" do something if starts with "53" something
the code work if the records are orders like 51 52 53 but if it's 51 51 52 52 53 it select the first record and stop
I want it to select the First record that Starts with (51), (52),(53) if there any
this is the code
<pre>Public Sub StartAllSend()
Dim currRowNum As String
Dim currRowAmount As String
Dim currVRowNum As String
Dim currVRowAmount As String
Dim currERowNum As String
Dim currERowAmount As String
Dim r As Integer
Dim SrCell As String
<pre>If InvoiceDataGridView.Rows.Count > 1 Then
For Each row As DataGridViewRow In InvoiceDataGridView.Rows
If Not row.IsNewRow Then
SrCell = InvoiceDataGridView.Rows(r).Cells(1).Value
If SrCell <> "" Then
If SrCell.StartsWith("51") Then
If txtMobiNorNumber.Text = "" And cmdUSSD.Enabled = True Then
'InvoiceDataGridView.Rows(r).Selected = True
lblMNo.Text = 0
currRowAmount = InvoiceDataGridView.CurrentRow.Cells(3).Value
currRowNum = InvoiceDataGridView.CurrentRow.Cells(1).Value
currRowUser = "Buy"
currRowUserID = InvoiceDataGridView.CurrentRow.Cells(0).Value
txtMobiNorNumber.Text = currRowNum
cboMobiNorAmount.Text = (Int(currRowAmount))
InvoiceDataGridView.Rows.Remove(InvoiceDataGridView.Rows(r))
End If
ElseIf SrCell.StartsWith("52") Then
If txtMobiNorNumber2.Text = "" And cmdUSSD2.Enabled = True Then
'InvoiceDataGridView.Rows(r).Selected = True
lblVNo.Text = 0
currVRowAmount = InvoiceDataGridView.CurrentRow.Cells(3).Value
currVRowNum = InvoiceDataGridView.CurrentRow.Cells(1).Value
currVRowUser = "Sell"
currVRowUserID = InvoiceDataGridView.CurrentRow.Cells(0).Value
txtMobiNorNumber2.Text = currVRowNum
cboMobiNorAmount2.Text = (Int(currVRowAmount))
InvoiceDataGridView.Rows.Remove(InvoiceDataGridView.Rows(r))
End If
ElseIf SrCell.StartsWith("53") Then
If txtMobiNorNumber3.Text = "" And cmdUSSD3.Enabled = True Then
' InvoiceDataGridView.Rows(r).Selected = True
lblENo.Text = 0
currERowAmount = InvoiceDataGridView.CurrentRow.Cells(3).Value
currERowNum = InvoiceDataGridView.CurrentRow.Cells(1).Value
currERowUser = "Empty"
currERowUserID = InvoiceDataGridView.CurrentRow.Cells(0).Value
txtMobiNorNumber3.Text = currERowNum
cboMobiNorAmount3.Text = (Int(currERowAmount))
InvoiceDataGridView.Rows.Remove(InvoiceDataGridView.Rows(r))
End If
End If
End If
End If
Next
End If
End Sub<pre>
What I have tried:
I want it to select the First record that Starts with (51), (52),(53) if there any
推荐答案
Private Sub btnSearchDGV_Click(sender As Object, e As EventArgs) Handles btnSearchDGV.Click
'This function will locate the FIRST row starting with 51, 52 or 53
'It will then handle that row setting some other control values
'It will then delete the row that it found
Dim rowToDelete = -1
For Each r As DataGridViewRow In InvoiceDataGridView.Rows
Dim cValue As String = r.Cells(1).Value.ToString()
If cValue.StartsWith("51") Or cValue.StartsWith("52") Or cValue.StartsWith("53") Then
Select Case cValue.Substring(0, 2)
Case "51"
Handle51()
Case "52"
Handle52()
Case "53"
Handle53()
End Select
rowToDelete = r.Index
Exit For 'So we only handle the first one we find
End If
Next
'Delete the row we have just processed, if we have processed one
If rowToDelete > -1 Then
InvoiceDataGridView.Rows.RemoveAt(rowToDelete)
End If
End Sub
或者,如果你想要遍历所有以51,52或53开头的行...
Alternatively, if you want to step through all the rows that start with 51, 52 or 53...
Private Sub btnSearchAllDGV_Click(sender As Object, e As EventArgs) Handles btnSearchAllDGV.Click
'This function will locate ALL the rows starting with 51, 52 or 53
'It will then handle each row setting some other control values
'It will then delete each row that it found
For row As Integer = InvoiceDataGridView.Rows.Count - 1 To 0 Step -1
Dim cValue As String
If Not IsNothing(InvoiceDataGridView.Rows(row).Cells(1).Value) Then
cValue = InvoiceDataGridView.Rows(row).Cells(1).Value.ToString()
If cValue.StartsWith("51") Or cValue.StartsWith("52") Or cValue.StartsWith("53") Then
Select Case cValue.Substring(0, 2)
Case "51"
Handle51()
Case "52"
Handle52()
Case "53"
Handle53()
End Select
InvoiceDataGridView.Rows.RemoveAt(row)
End If
End If
Next
End Sub
请注意,子程序Handle51,Handle52和Handle53将是您设置其他文本框等内容的位置。请勿尝试从这些子程序中的DataGridView中删除任何行。
Note that the subroutines Handle51, Handle52 and Handle53 would be where you set the contents of the other textboxes etc. Do NOT attempt to remove any rows from the DataGridView in those subroutines.
这篇关于Datagridview循环搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!