Datagridview delete rows

  • Thread starter Thread starter friend
  • Start date Start date
F

friend

hi all,

I am filling the datagridview from the text boxes.
that means i enter the values in the text boxes, by clicking a button
all the values in the text boxes get stored in datagridview.

so my gridview is a below

1 2 3
4 5 6
......

if i want to delete the selected row from the datagridview i am trying
in the following way...

For Each row1 As DataGridViewRow In Me.DataGridView1.SelectedRows
Me.DataGridView1.Rows.RemoveAt(row1.Index)
Next

but it doesnt work....it is giving an error relates to the
ibindungslist.
 
friend said:
I am filling the datagridview from the text boxes.
that means i enter the values in the text boxes, by clicking a button
all the values in the text boxes get stored in datagridview.

so my gridview is a below

1 2 3
4 5 6
.....

if i want to delete the selected row from the datagridview i am trying
in the following way...

For Each row1 As DataGridViewRow In Me.DataGridView1.SelectedRows
Me.DataGridView1.Rows.RemoveAt(row1.Index)
Next

but it doesnt work....it is giving an error relates to the
ibindungslist.

What if you get a list of the selected rows' indexes and remove them
starting from the last one down to the first?

Otherwise, when you remove one from near the beginning, the indexes of all
the ones after that get changed.

Andrew
 
What if you get a list of the selected rows' indexes and remove them
starting from the last one down to the first?

Otherwise, when you remove one from near the beginning, the indexes of all
the ones after that get changed.

Andrew

The thing is...I have the data in datagridview...
and i select one from those rows...by clicking some button or
other...the selected row should get deleted.

thanks
 
hi all,

I am filling the datagridview from the text boxes.
that means i enter the values in the text boxes, by clicking a button
all the values in the text boxes get stored in datagridview.

so my gridview is a below

1 2 3
4 5 6
.....

if i want to delete the selected row from the datagridview i am trying
in the following way...

For Each row1 As DataGridViewRow In Me.DataGridView1.SelectedRows
Me.DataGridView1.Rows.RemoveAt(row1.Index)
Next

but it doesnt work....it is giving an error relates to the
ibindungslist.

You can not modify a list while you are traversing it with For Each.
Use a regular For instead, and go from the end to the beginning:

With Me.DataGridView1
For indx As Integer = .SelectedRows.Count-1 To 1 Step -1
.Rows.RemoveAt(.SelectedRows[indx].Index)
Next
End With
 
Jack Jackson said:
hi all,

I am filling the datagridview from the text boxes.
that means i enter the values in the text boxes, by clicking a button
all the values in the text boxes get stored in datagridview.

so my gridview is a below

1 2 3
4 5 6
.....

if i want to delete the selected row from the datagridview i am trying
in the following way...

For Each row1 As DataGridViewRow In Me.DataGridView1.SelectedRows
Me.DataGridView1.Rows.RemoveAt(row1.Index)
Next

but it doesnt work....it is giving an error relates to the
ibindungslist.

You can not modify a list while you are traversing it with For Each.
Use a regular For instead, and go from the end to the beginning:

With Me.DataGridView1
For indx As Integer = .SelectedRows.Count-1 To 1 Step -1
.Rows.RemoveAt(.SelectedRows[indx].Index)
Next
End With

It needs to go to 0 not 1:
For nRow = DataGridView1.SelectedRows.Count - 1 To 0 Step -1
DataGridView1.Rows.Remove(DataGridView1.SelectedRows(nRow))
Next
 
Back
Top