开发者

Checking if a text on a label exists in datagridview

开发者 https://www.devze.com 2023-01-05 02:20 出处:网络
Can somebody help me with this? I am adding the text of a label into a datagridview. But before i do the insert, i want to check if the text does not already exist in the datagridview. I am stack. Ne

Can somebody help me with this?

I am adding the text of a label into a datagridview. But before i do the insert, i want to check if the text does not already exist in the datagridview. I am stack. Need h开发者_运维知识库elp.

Thanks in advance.


You could just loop through the rows and cells of the DataGridView and compare the strings, something like:

Private Sub AddLabelToDGV(ByVal dgv As DataGridView, ByVal labelText As String)
    Dim found As Boolean = False
    For Each row As DataGridViewRow In dgv.Rows
        For Each cell As DataGridViewCell In row.Cells
            If cell.Value IsNot Nothing AndAlso cell.Value.ToString().Equals(labelText) Then
                found = True ' if this is found it might be worth exiting the loop now instead of continuing
            End If
        Next
    Next
    If Not found Then
        Dim row As New DataGridViewRow
        row.CreateCells(dgv)
        row.Cells(0).Value = labelText
        dgv.Rows.Add(row)
    End If
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号