how to update data in a datagridview to a database

Answered how to update data in a datagridview to a database

  • 2012년 5월 9일 수요일 오전 8:33
     
      코드 있음

    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 Sub


    shehzi

모든 응답

  • 2012년 5월 10일 목요일 오전 6:08
    중재자
     
     답변됨 코드 있음

    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


  • 2012년 5월 10일 목요일 오전 7:08
     
     

    hi jason wang

    thanks a lot :)

    problem solved :)


    shehzi