I am using vb.net. I have following weird problems:
If I comment DGVCusClient.Rows.Add(), the cell in (“column1”,0) does not display data. But in debug, I can see that the first cell has data assigned.
If I do not comment DGVCusClient.Rows.Add(), the cell in (“column1”,0) displays its data correctly. However, it adds the row on the top for the first time. Except for the first row, it adds rows to the bottom as usual.
Dim i As Integer = DGVCusClient.CurrentRow.Index
If Not ContainRecord(tempCusid, tempCltid) Then
Dim i As Integer = DGVCusClient.CurrentRow.Index
DGVCusClient.Item("Column1", i).Value = "a"
DGVCusClient.Item("Column2", i).Value = "b"
'DGVCusClient.Rows.Add()
End If
Private Function ContainRecord(ByVal strCusid As String, ByVal strCltid As String) As Boolean
For i As Integer = 0 To DGVCusClient.Rows.Count - 1
If Not DGVCusClient.Item("Column1", i).Value Is Nothing AndAlso Not DGVCusClient.Item("Column2", i).Value Is Nothing Then
If DGVCusClient.Item("Column1", i).Value.ToString = strCusid AndAlso DGVCusClient.Item("Column2", i).Value.ToString = strCltid Then
Return True
End If
End If
Next
Return False
End Function
The following code can always add rows to the bottom, which is what I want. So I just need to first add new row, and then set values to current cells.