deletion of a row in datagrid

  • Thread starter Thread starter latin & geek via DotNetMonster.com
  • Start date Start date
L

latin & geek via DotNetMonster.com

hi!

im relatively new to .net, and am currently battling with this datagrid
business. ive used a dataset and dataview to present the information in the
datagrid. im able to add and edit rows, but have no idea how to delete 'em
using code.

....or is there any way to at least get the selected row index number so that
i can pass a delete command to the dataset and update the access table from
that?

can some kindly soul please help me?

thanks very much!
 
latin said:
hi!

im relatively new to .net, and am currently battling with this datagrid
business. ive used a dataset and dataview to present the information in the
datagrid. im able to add and edit rows, but have no idea how to delete 'em
using code.

...or is there any way to at least get the selected row index number so that
i can pass a delete command to the dataset and update the access table from
that?

can some kindly soul please help me?

thanks very much!

DataGrid.CurrentRowIndex

Yes, deleting it from the datasource is the way to remove it.

Chris
 
Latin,

In addidion to Chris, (why are there so many Chrisses active the last time).

Try to use forever the underlying DataSource if you are acting with a
complex datacontrol as the DataGrid or DataGridView.

You know that we here as default is a windowsform application.

I hope this helps,

Cor
 
Dear Chris & Cor,

Thank you folks very much for the immediate response! i tried this:

Dim rowin As Integer
rowin = DataGrid1.CurrentRowIndex
ds2.Tables("wex").Rows(rowin).Delete()
da2.Update(ds2, "wex")
DataGrid1.Refresh()

i get this error -

Error: system.data:update requires a valid delete command, when passed
datarow collection with deleted rows

what have i done wrong?
 
Latin,

This is as it say.
Error: system.data:update requires a valid delete command, when passed
datarow collection with deleted rows
Mostly you can build those using the commandbuilder wich can have the prefix
SQL, OleDB etc.
Dim rowin As Integer
rowin = DataGrid1.CurrentRowIndex
ds2.Tables("wex").Rows(rowin).Delete()

dim cmb as new XXXCommandBuilder(Da2)
da2.Update(ds2, "wex")
DataGrid1.Refresh()
Probably is this enough, however you can of course better set it a place
that it is not done with every delete, one time is normally enough if the DA
has its information as the selectstring and the connection.

Try the currencymanager for getting the right row, it is give you mostly
better results.

http://www.vb-tips.com/default.aspx?ID=5f4a0f68-a3b6-4fc8-8aff-587f730fa118

I hope this helps,

Cor
 
cor, thanks a LOT! it works now :D

in case anyone needs the code:

Dim rowin as Integer
Dim cmb As New OleDbCommandBuilder(da2)
rowin = DataGrid1.CurrentRowIndex

' Get the Currency Manager by using the BindingContext of the
DataGrid
Dim cm As CurrencyManager = CType(Me.BindingContext(DataGrid1.
DataSource, DataGrid1.DataMember), CurrencyManager)

' Retrieve the default DataView of the DataGrid
Dim dvdel As DataView = CType(cm.List, DataView)

' Use Currency Manager and DataView to retrieve the Current Row
Dim dr As DataRow
dr = dvdel.Item(cm.Position).Row
Try
dr.Delete()
da2.Update(ds2, "wex")
DataGrid1.Refresh()
Catch ex As Exception
MsgBox("Error: " & ex.Source & ":" & ex.Message, MsgBoxStyle.
OKOnly)
End Try
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top