Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Insert new row to master - detail form using bindingsource

$
0
0

Hi all,

I have a form to analyse price of product, the form include master (textbox:slip ID,datetimepicker : date) and detail(datagridview:slipID,productID,price..)

When I insert new row as below code, all inserted to DB but not show on form, I must close and reopen the form the new row will show, how i must to do to fix it 

please help, thanks

Select data when form load 

 ds = New DataSet
        damaster = New SqlDataAdapter("select soPTG,ngaybd,ngaykt,khoa = case when sttesign =0 then 'True' else 'False' end from tbl_PTGMaster", conn)
        damaster.Fill(ds, "tbl_ptgMaster")
        dadet = New SqlDataAdapter("select [SoPTG],[Mahang],[gianhap],[thuesuat],[laigop],[giacu],[giamoi] from tbl_PTGDetail", conn)
        dadet.Fill(ds, "tbl_ptgdetail")
        ds.Relations.Add("PTG_rel", ds.Tables("tbl_ptgmaster").Columns("SoPTG"), ds.Tables("tbl_ptgdetail").Columns("SoPTG")).ChildKeyConstraint.UpdateRule = Rule.Cascade
        BindingMaster.DataSource = ds
        BindingMaster.DataMember = "tbl_ptgmaster"
        BindingDetail.DataSource = BindingMaster
        BindingDetail.DataMember = "PTG_rel"
        gridchitiet.DataMember = "tbl_ptgdetail"
        txtptgnumb.DataBindings.Add("text", BindingMaster, "soPTG")
        dtstart.DataBindings.Add("Text", BindingMaster, "ngaybd")
        dtend.DataBindings.Add("Text", BindingMaster, "ngaykt")
        cmdlock.DataBinding.Add("Enabled", BindingMaster, "khoa", True, DataSourceUpdateMode.OnPropertyChanged)
        setStatus()
        SetHeader()

  Dim cmdinsert As New SqlCommand("insert into tbl_PTGMaster(SoPTG,ngaybd,ngaykt) values(@SoPTG,@ngaybd,@ngaykt)", conn)
  cmdinsert.Parameters.Add("@soPTG", SqlDbType.Char, 10).Value = txtptgnumb.Text
            cmdinsert.Parameters.Add("@ngaybd", SqlDbType.DateTime).Value = dtstart.Value
            cmdinsert.Parameters.Add("@ngaykt", SqlDbType.DateTime).Value = dtend.Value
            damaster.InsertCommand = cmdinsert
            damaster.FillSchema(ds, SchemaType.Source)
            cmdinsert = New SqlCommand("insert into tbl_PTGDetail(SoPTG,mahang,gianhap) values(@SoPTG,@mahang,@gianhap)", conn)
            cmdinsert.Parameters.Add("@SoPTG", SqlDbType.Char, 10).Value = txtptgnumb.Text
            cmdinsert.Parameters("@SoPTG").Direction = ParameterDirection.InputOutput
            cmdinsert.Parameters.Add("@mahang", SqlDbType.Char, 10, "mahang")
            cmdinsert.Parameters.Add("@gianhap", SqlDbType.Decimal, 8, "gianhap")
            dadet.InsertCommand = cmdinsert
            dadet.FillSchema(ds, SchemaType.Source)
            gridchitiet.FinishEditing()
            BindingMaster.EndEdit()
            damaster.Update(ds, "tbl_PTGMaster")
            ds.Tables("tbl_PTGMaster").AcceptChanges()

            BindingDetail.EndEdit()
            dadet.Update(ds, "tbl_PTGDetail")
            ds.Tables("tbl_PTGDetail").AcceptChanges()
            BindingMaster.ResetBindings(False)
            BindingDetail.ResetBindings(False)


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>