deleting a row in datagrid

srivalli

Well-known member
Joined
May 4, 2005
Messages
189
Programming Experience
Beginner
in my form i have a datagrid
for inserting a row in a datagrid i use the following code

dim da as new sqldataadapter("select * from tab",cn)
dim x as new sqlcommnadbuilder(da)
da.insertcommnad=x.getinsertcommand()
da.update(ds,"t")

it is working fine
also i wrote code for updating
just replace updatecommand with insert command

it is also working fine

now my query is i want to delete a row from data grids using sqlcommandbuilder

i tried like this

dim da as new sqldataadapter("select * from tab",cn)
dim x as new sqlcommnadbuilder(da)
da.deletecommnad=x.getdeletecommand()
da.update(ds,"t")

but the row is not getting deleted

what r the modifications needed
thk u
 
If you use a CommandBuilder you don't need to assign the Commands it creates to the DataAdapter. You simly create a new CommandBuilder for a particular DataApater and call Update. The commands are created, assigned and executed for you. To delete a row, it must have been marked as deleted first. This done by you calling Delete on the DataRow concerned.

Edit:
VB.NET:
		Dim myConnection As New SqlConnection
		Dim myAdapter As New SqlDataAdapter("SELECT * FROM Table1", myConnection)
		Dim myTable As New DataTable("Table1")

		myAdapter.Fill(myTable)

		'Edit rows here.
		myTable.Rows(0).Delete()

		Dim myBuilder As New SqlCommandBuilder(myAdapter)

		myAdapter.Update(myTable)
Also, it doesn't matter when or where you create the command builder. The commands will always be generated when you call Update.
 
thks

thank u for ur response

i ll try this and let u know the output

jmcilhinney said:
If you use a CommandBuilder you don't need to assign the Commands it creates to the DataAdapter. You simly create a new CommandBuilder for a particular DataApater and call Update. The commands are created, assigned and executed for you. To delete a row, it must have been marked as deleted first. This done by you calling Delete on the DataRow concerned.

Edit:
VB.NET:
		Dim myConnection As New SqlConnection
		Dim myAdapter As New SqlDataAdapter("SELECT * FROM Table1", myConnection)
		Dim myTable As New DataTable("Table1")
 
		myAdapter.Fill(myTable)
 
		'Edit rows here.
		myTable.Rows(0).Delete()
 
		Dim myBuilder As New SqlCommandBuilder(myAdapter)
 
		myAdapter.Update(myTable)
Also, it doesn't matter when or where you create the command builder. The commands will always be generated when you call Update.
 
Back
Top