how to update data in a datagridview to a database
-
Wednesday, May 09, 2012 8:33 AM
i have 2 tables- invoice[ino,date,name,address,total,tax1,tax2] and invoicedetails [ino,product,quantity,rate,amount]. i have a invoicedetails datagridview.
i want to update the data into a database. i tried the following method, but it is not getting added to the database. please help me with it. thanks in advance.
Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click If InvoicedetailsDataGridView.Rows.Count > 0 Then Me.AddInvoice() Me.UpdateDatabase() 'Me.Close() Else MessageBox.Show("You must add at least one line item " _ & "before saving the invoice.", "Invoice Not Saved") End If End Sub Private Sub AddInvoice() Try Dim invoiceRow As invoicestockDataSet.invoiceRow = Me.InvoicestockDataSet.invoice.NewinvoiceRow invoiceRow._date = DateDateTimePicker.Value invoiceRow.name = CStr(NameTextBox.Text) invoiceRow.address = CStr(AddressTextBox.Text) invoiceRow.total = productTotal invoiceRow.tax1 = tax1 invoiceRow.tax2 = tax2 Me.InvoicestockDataSet.invoice.AddinvoiceRow(invoiceRow) Catch ex As Exception MessageBox.Show(ex.Message, ex.GetType.ToString) End Try End Sub Private Sub UpdateDatabase() Try ' Update the Invoices table. InvoiceTableAdapter.Update(Me.InvoicestockDataSet.invoice) ' Get the final invoice ID. Dim invoiceID As Integer = CInt(InvoiceTableAdapter.GetLastIdentityValue) ' Set the final invoice ID for all line items. For Each row As invoicestockDataSet.invoicedetailsRow In Me.InvoicestockDataSet.invoicedetails row.ino = invoiceID Next ' Update the InvoiceLineItems table. InvoicedetailsTableAdapter.Update(Me.InvoicestockDataSet.invoicedetails) MsgBox("Invoice Saved") Catch ex As SqlException MessageBox.Show("Database error # " & ex.Number & "; " & ex.Message, ex.GetType.ToString) End Try End Subshehzi
All Replies
-
Thursday, May 10, 2012 6:08 AMModerator
Hi guy,
I have a series of ways to rebind this data as follows:
1. Using TableAdapter.Fill(this.DataSet.DataTable) to update the data in a DataGridView control.
2.Using BindingSource control:
bs = new BindingSource(); bs.DataSource = ds.Tables["table name"]; dataGridView1.DataSource = bs.DataSource;
3.Using INotifyPropertyChanged to refresh the data.
Sincerely,
Jason Wang
Jason Wang [MSFT]
MSDN Community Support | Feedback to us
- Edited by Jason Dot WangMicrosoft Contingent Staff, Moderator Thursday, May 10, 2012 6:09 AM
- Marked As Answer by shezteq Thursday, May 10, 2012 7:07 AM
-
Thursday, May 10, 2012 7:08 AM
hi jason wang
thanks a lot :)
problem solved :)
shehzi


