add new row if not exist row in datagirdview1
-
my code
If DataGridView1.Rows.Count - 1 Then
DataGridView1.Rows.Add() DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(1) DataGridView1.BeginEdit(True) Else Exit Sub ' DataGridView1.Rows.Add() End If
i many code try
If (DataGridView1.Rows.Count > 0) Then For i = 0 To DataGridView1.Rows.Count - 1 If DataGridView1.Rows(i).Cells("d1alisname").Value = "" Then Exit Sub Else DataGridView1.Rows.Add() End If Next End If
but add row
-
my code
If DataGridView1.Rows.Count - 1 Then
DataGridView1.Rows.Add() DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(1) DataGridView1.BeginEdit(True) Else Exit Sub ' DataGridView1.Rows.Add() End If
i many code try
If (DataGridView1.Rows.Count > 0) Then For i = 0 To DataGridView1.Rows.Count - 1 If DataGridView1.Rows(i).Cells("d1alisname").Value = "" Then Exit Sub Else DataGridView1.Rows.Add() End If Next End If
but add row
-
my code
If DataGridView1.Rows.Count - 1 Then
DataGridView1.Rows.Add() DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(1) DataGridView1.BeginEdit(True) Else Exit Sub ' DataGridView1.Rows.Add() End If
i many code try
If (DataGridView1.Rows.Count > 0) Then For i = 0 To DataGridView1.Rows.Count - 1 If DataGridView1.Rows(i).Cells("d1alisname").Value = "" Then Exit Sub Else DataGridView1.Rows.Add() End If Next End If
but add row
Sufvan Adil 2023 wrote:
If DataGridView1.Rows.Count - 1 Then
Thanks to VB's "evil type coercion", that condition will be
False
if you have precisely one row in your grid. For any other number of rows, that condition will beTrue
.Sufvan Adil 2023 wrote:
For i = 0 To DataGridView1.Rows.Count - 1
If DataGridView1.Rows(i).Cells("d1alisname").Value = "" Then
Exit Sub
Else
DataGridView1.Rows.Add()
End If
NextThat code walks through all the rows in your grid until it finds one where the
d1alisname
column is blank. For every row that it encounters where that column is not blank, it adds a new row. You haven't explained exactly what you're trying to do. At a guess, you want to add a new row if there are either no rows in the grid, or all of the rows have a value in thed1alisname
column. In which case, you need to move the "add row" code after yourFor
loop from your second code block:For i = 0 To DataGridView1.Rows.Count - 1
If DataGridView1.Rows(i).Cells("d1alisname").Value = "" Then
Exit Sub
End If
NextDataGridView1.Rows.Add()
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer