检查datagridview中是否存在标签上的文本

时间:2010-06-27 22:06:02

标签: vb.net

有人可以帮我吗?

我正在将标签的文本添加到datagridview中。但在我进行插入之前,我想检查datagridview中是否还没有文本。我是堆栈。需要帮助。

提前致谢。

1 个答案:

答案 0 :(得分:0)

你可以循环遍历DataGridView的行和单元格并比较字符串,例如:

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