Windows Develop Bookmark and Share   
 index > Windows Forms General > add/edit and delete functions to datagridview
 

add/edit and delete functions to datagridview

Hi

I use this sub to fill a datagridview, it works fine. But how do add edit/add and delete functions to it?

Sub BindGrid()
Dim sqlStr As String = "SELECT * FROM tbl_Work"
Dim conn As OleDbConnection = New OleDbConnection(MyProviderString)
Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)
Dim ds As DataSet = New DataSet
da.Fill(ds, "tbl_Work")
Dim dv As DataView = ds.Tables("tbl_Work").DefaultView
DataGridView1.DataSource = ds.Tables("tbl_Work").DefaultView
End Sub

Regards

magmo  Sunday, September 30, 2007 5:19 PM

Hi

Did you mean writing data back into the database? If so, there are two ways to achieve this.

First, if the tbl_Work table has a primary key or a unique column, you can use a CommanderBuilder object to auto-generate the InsertCommand, UpdateCommand, and DeleteCommand for the DataAdapter. Check this page for a sample.

Second, you can create your own InsertCommand, UpdateCommand, and DeleteCommand for the DataAdapter. Try something like the following:

Code Block

Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)

da.DeleteCommand = New OleDbCommand("delete ...", conn)

da.InsertCommand = New OleDbCommand("insert ...", conn)

da.DeleteCommand = New OleDbCommand("update ...", conn)

You need to call Update method for the DataAdapter when you want to write back to the database.

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Tuesday, October 02, 2007 8:26 AM

Hi

Did you mean writing data back into the database? If so, there are two ways to achieve this.

First, if the tbl_Work table has a primary key or a unique column, you can use a CommanderBuilder object to auto-generate the InsertCommand, UpdateCommand, and DeleteCommand for the DataAdapter. Check this page for a sample.

Second, you can create your own InsertCommand, UpdateCommand, and DeleteCommand for the DataAdapter. Try something like the following:

Code Block

Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)

da.DeleteCommand = New OleDbCommand("delete ...", conn)

da.InsertCommand = New OleDbCommand("insert ...", conn)

da.DeleteCommand = New OleDbCommand("update ...", conn)

You need to call Update method for the DataAdapter when you want to write back to the database.

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Tuesday, October 02, 2007 8:26 AM

You can use google to search for other answers

Custom Search

More Threads

• How to group controls
• Updating Values on Multipule Forms
• Dll reference issue
• Control visiblilty of main form component after the close of pop-up win form
• Problem in using waitcursor
• Zoom event for picturebox
• When adding a UserControl it is placed behind other controls.
• Compile J2EE in Visual Studio 2003 and create exe of .Net
• right justification of a format string problem
• How to add Items to a Specific Group and Column?