如何在vb.net中的子表中插入,更新和删除数据后如何引用datagridview [英] how to referesh datagridview after insert,update and delete data into child table in vb.net
问题描述
大家好,
我有一个带有一个datagridview的主从表单,如下所示:
i have a master-detail form with one datagridview as follow:
这是主表加载代码>>
this is master table loading code>>
私有 子 load_sanad_header()
Private Sub load_sanad_header()
'获取数据.
' Get the data.
尝试
Try
暗 daheader 如 新建 SqlDataAdapter ("选择id_sanad,日期, fari_no,provided_at,sanad_type,sanad_state,sader_konnande,tayid_konnande from sanad_header"",con)
Dim daheader As New SqlDataAdapter("Select id_sanad,date,fari_no,provided_at,sanad_type,sanad_state,sader_konnande,tayid_konnande from sanad_header", con)
暗 dadetails 如 新 SqlDataAdapter (从sanad_hesabdari中选择id_sanad,id_moin,sathe_4 sathe_5,sathe_6,sharh,bed,bes,arz_name" ,con)
Dim dadetails As New SqlDataAdapter("select id_sanad,id_moin,sathe_4 sathe_5,sathe_6,sharh,bed,bes,arz_name from sanad_hesabdari", con)
ds = 新建 数据集()
ds = New DataSet()
daheader.Fill(ds,"sanad_header" )
daheader.Fill(ds, "sanad_header")
dadetails.Fill(ds,"sanad_hesabdari" )
dadetails.Fill(ds, "sanad_hesabdari")
ds.Relations.Add("sanad" ,ds.Tables("sanad_header" ).Columns("id_sanad" ),ds.Tables("sanad_hesabdari" ).Columns("id_sanad" ))
ds.Relations.Add("sanad", ds.Tables("sanad_header").Columns("id_sanad"), ds.Tables("sanad_hesabdari").Columns("id_sanad"))
绑定控件.
'Bind the controls.
Label153.DataBindings.Add(文本" ,ds.Tables("sanad_header" ), "id_sanad" )
Label153.DataBindings.Add("Text", ds.Tables("sanad_header"), "id_sanad")
Label147.DataBindings.Add(文本" ,ds.Tables("sanad_header" ), 日期" )
Label147.DataBindings.Add("Text", ds.Tables("sanad_header"), "date")
Label146.DataBindings.Add(文本" ,ds.Tables("sanad_header" ), "fari_no" )
Label146.DataBindings.Add("Text", ds.Tables("sanad_header"), "fari_no")
Label158.DataBindings.Add(文本" ,ds.Tables("sanad_header" ), " provided_at" )
Label158.DataBindings.Add("Text", ds.Tables("sanad_header"), "provided_at")
Label138.DataBindings.Add(文本" ,ds.Tables("sanad_header" ), "sanad_type" )
Label138.DataBindings.Add("Text", ds.Tables("sanad_header"), "sanad_type")
Label145.DataBindings.Add(文本" ,ds.Tables("sanad_header" ), "sanad_state" )
Label145.DataBindings.Add("Text", ds.Tables("sanad_header"), "sanad_state")
Label151.DataBindings.Add(文本" ,ds.Tables("sanad_header" ), "sader_konnande" )
Label151.DataBindings.Add("Text", ds.Tables("sanad_header"), "sader_konnande")
Label150.DataBindings.Add(文本" ,ds.Tables("sanad_header" ), "tayid_konnande" )
Label150.DataBindings.Add("Text", ds.Tables("sanad_header"), "tayid_konnande")
Label149.DataBindings.Add(文本" ,ds.Tables("sanad_hesabdari" ), "id_sanad" )
Label149.DataBindings.Add("Text", ds.Tables("sanad_hesabdari"), "id_sanad")
DataGridView16.DataSource = ds.Tables("sanad_header" )
DataGridView16.DataSource = ds.Tables("sanad_header")
DataGridView16.DataMember = "sanad"
DataGridView16.DataMember = "sanad"
DataGridView16.Columns(0).Visible = 错误
DataGridView16.Columns(0).Visible = False
'初始化CurrencyManager.
'Initialize the CurrencyManager.
cm = CType ( Me .BindingContext(ds.Tables()), CurrencyManager )
cm = CType(Me.BindingContext(ds.Tables("sanad_header")), CurrencyManager)
捕获,例如 As 异常
Catch ex As Exception
MsgBox(ex.Message)
MsgBox(ex.Message)
结束 尝试
End Try
结束 子
End Sub
这是明细表加载代码>>
and this is detail table loading code>>
私有 子 load_sanad_hesabdari()
Private Sub load_sanad_hesabdari()
尝试
Try
昏暗 daCust 为 新建 SqlDataAdapter ("选择* from sanad_header" ,con)
Dim daCust As New SqlDataAdapter("Select * from sanad_header", con)
暗 daOrders 按 新建 SqlDataAdapter ("选择id_sanad,id_moin, sathe_4,sathe_5,sathe_6,sharh,bed,bes,arz_name来自sanad_hesabdari" ,con)
Dim daOrders As New SqlDataAdapter("select id_sanad,id_moin,sathe_4,sathe_5,sathe_6,sharh,bed,bes,arz_name from sanad_hesabdari", con)
ds = 新建 数据集()
ds = New DataSet()
daCust.Fill(ds,"sanad_header" )
daCust.Fill(ds, "sanad_header")
daOrders.Fill(ds,"sanad_hesabdari" )
daOrders.Fill(ds, "sanad_hesabdari")
ds.Relations.Add("sanad" ,ds.Tables("sanad_header" ).Columns("id_sanad" ),ds.Tables("sanad_hesabdari" ).Columns("id_sanad" ))
ds.Relations.Add("sanad", ds.Tables("sanad_header").Columns("id_sanad"), ds.Tables("sanad_hesabdari").Columns("id_sanad"))
DataGridView1.DataSource = ds.Tables("sanad_header" )
DataGridView1.DataSource = ds.Tables("sanad_header")
DataGridView1.DataMember = "sanad"
DataGridView1.DataMember = "sanad"
DataGridView1.Columns(0).Visible = 错误
DataGridView1.Columns(0).Visible = False
初始化CurrencyManager.
'Initialize the CurrencyManager.
cd = CType ( Me .BindingContext(ds.Tables()), CurrencyManager )
cd = CType(Me.BindingContext(ds.Tables("sanad_header")), CurrencyManager)
如果 不是(cd 是 没什么) 并且还 cd.Count 然后
If Not (cd Is Nothing) AndAlso cd.Count Then
cd.Position = cd.Count-1
cd.Position = cd.Count - 1
结束 如果
End If
绑定控件.
'Bind the controls.
我 .TextBox5.DataBindings.Add(新建 绑定(文本" ,ds.Tables(), "id_sanad" , True , DataSourceUpdateMode .OnValidation))
Me.TextBox5.DataBindings.Add(New Binding("Text", ds.Tables("sanad_header"), "id_sanad", True, DataSourceUpdateMode.OnValidation))
我 .TextBox1.DataBindings.Add(新建 绑定(文本" ,ds.Tables(), 日期" ,真, DataSourceUpdateMode .OnValidation))
Me.TextBox1.DataBindings.Add(New Binding("Text", ds.Tables("sanad_header"), "date", True, DataSourceUpdateMode.OnValidation))
我 .ComboBox1.DataBindings.Add(新建 绑定(文本" ,ds.Tables(), "sanad_type" , True , DataSourceUpdateMode .OnValidation))
Me.ComboBox1.DataBindings.Add(New Binding("Text", ds.Tables("sanad_header"), "sanad_type", True, DataSourceUpdateMode.OnValidation))
我 .TextBox3.DataBindings.Add(新建 绑定(文本" ,ds.Tables(), "sanad_state" , True , DataSourceUpdateMode .OnValidation))
Me.TextBox3.DataBindings.Add(New Binding("Text", ds.Tables("sanad_header"), "sanad_state", True, DataSourceUpdateMode.OnValidation))
我 .TextBox6.DataBindings.Add(新建 绑定(文本" ,ds.Tables(), 拉丁" ,真, DataSourceUpdateMode .OnValidation))
Me.TextBox6.DataBindings.Add(New Binding("Text", ds.Tables("sanad_header"), "latin", True, DataSourceUpdateMode.OnValidation))
我 .TextBox7.DataBindings.Add(新建 绑定(文本" ,ds.Tables(), "fari_no" , True , DataSourceUpdateMode .OnValidation))
Me.TextBox7.DataBindings.Add(New Binding("Text", ds.Tables("sanad_header"), "fari_no", True, DataSourceUpdateMode.OnValidation))
我 .TextBox2.DataBindings.Add(新建 绑定(文本" ,ds.Tables(), "roozane_no" , True , DataSourceUpdateMode .OnValidation))
Me.TextBox2.DataBindings.Add(New Binding("Text", ds.Tables("sanad_header"), "roozane_no", True, DataSourceUpdateMode.OnValidation))
我 .TextBox8.DataBindings.Add(新建 绑定(文本" ,ds.Tables(), "sharh" , True , DataSourceUpdateMode .OnValidation))
Me.TextBox8.DataBindings.Add(New Binding("Text", ds.Tables("sanad_header"), "sharh", True, DataSourceUpdateMode.OnValidation))
我 .Label29.DataBindings.Add(新建 绑定(文本" ,ds.Tables(), "provided_at" , True , DataSourceUpdateMode .OnValidation))
Me.Label29.DataBindings.Add(New Binding("Text", ds.Tables("sanad_header"), "provided_at", True, DataSourceUpdateMode.OnValidation))
我 .TextBox4.DataBindings.Add(新建 绑定(文本" ,ds.Tables(), "atf_no" ,真, DataSourceUpdateMode .OnValidation))
Me.TextBox4.DataBindings.Add(New Binding("Text", ds.Tables("sanad_header"), "atf_no", True, DataSourceUpdateMode.OnValidation))
我 .Label30.DataBindings.Add(新建 绑定(文本" ,ds.Tables(), "sader_konnande" , True , DataSourceUpdateMode .OnValidation))
Me.Label30.DataBindings.Add(New Binding("Text", ds.Tables("sanad_header"), "sader_konnande", True, DataSourceUpdateMode.OnValidation))
我 .Label31.DataBindings.Add(新建 绑定(文本" ,ds.Tables(), " tayid_konnande" , True , DataSourceUpdateMode .OnValidation))
Me.Label31.DataBindings.Add(New Binding("Text", ds.Tables("sanad_header"), "tayid_konnande", True, DataSourceUpdateMode.OnValidation))
我 .Label32.DataBindings.Add(新建 绑定(文本" ,ds.Tables(), 分支" ,真, DataSourceUpdateMode .OnValidation))
Me.Label32.DataBindings.Add(New Binding("Text", ds.Tables("sanad_header"), "branch", True, DataSourceUpdateMode.OnValidation))
捕获 ex 为 SqlException
Catch ex As SqlException
MsgBox(ex.Message)
MsgBox(ex.Message)
结束 尝试
End Try
结束 子
End Sub
并且这是将数据代码插入到主表和明细表中>>
and also this is insert data code into master and detail tables>>
私有 子 Button12_Click( ByVal 发件人 按系统.对象, ByVal e As 系统. EventArgs ) 手柄 Button12.单击
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
尝试
Try
如果 TextBox1.Text<> "< 和 ComboBox1.Text<> " 和 TextBox3.Text<> " 和 TextBox5.Text = "" 然后
If TextBox1.Text <> "" And ComboBox1.Text <> "" And TextBox3.Text <> "" And TextBox5.Text = "" Then
cmd = 新建 SqlCommand (" ;INSERT INTO sanad_header values('" & TextBox1.Text & "','" & ComboBox1.Text& "','" & TextBox3.Text & "','" & TextBox8.Text & "','" & TextBox6.Text & "','" & TextBox2.Text & "','" & TextBox7.Text & "','" & Label31.Text & "','" & Label30.Text & "','" & Label29.Text & "','" & Label32.Text & "','" & TextBox4.Text & "')", con)
cmd = New SqlCommand("INSERT INTO sanad_header values('" & TextBox1.Text & "','" & ComboBox1.Text & "','" & TextBox3.Text & "','" & TextBox8.Text & "','" & TextBox6.Text & "','" & TextBox2.Text & "','" & TextBox7.Text & "','" & Label31.Text & "','" & Label30.Text & "','" & Label29.Text & "','" & Label32.Text & "','" & TextBox4.Text & "')", con)
con.Open()
con.Open()
cmd.ExecuteNonQuery()
cmd.ExecuteNonQuery()
If Not (cd Is Nothing) AndAlso cd.Count Then cd.Position = cd.Count - 1
If Not (cd Is Nothing) AndAlso cd.Count Then cd.Position = cd.Count - 1
con.Close()
con.Close()
其他
Else
Dim cmd1 As SqlCommand
Dim cmd1 As SqlCommand
If ComboBox13.Text <> "" And (TextBox34.Text <> "" Or TextBox73.Text <> "") Then
If ComboBox13.Text <> "" And (TextBox34.Text <> "" Or TextBox73.Text <> "") Then
cmd1 = New SqlCommand("INSERT INTO sanad_hesabdari values('" & TextBox5.Text & "','" & ComboBox13.Text & "','" & ComboBox14.Text & "','" & ComboBox15.Text & "','" & ComboBox23.Text & "','" & TextBox33.Text & "','" & TextBox34.Text & "','" & TextBox73.Text & "','" & TextBox74.Text & "')", con)
cmd1 = New SqlCommand("INSERT INTO sanad_hesabdari values('" & TextBox5.Text & "','" & ComboBox13.Text & "','" & ComboBox14.Text & "','" & ComboBox15.Text & "','" & ComboBox23.Text & "','" & TextBox33.Text & "','" & TextBox34.Text & "','" & TextBox73.Text & "','" & TextBox74.Text & "')", con)
con.Open()
con.Open()
cmd1.ExecuteNonQuery()
cmd1.ExecuteNonQuery()
'cd = CType(Me.BindingContext(DataGridView1.DataSource("sanad_header"), "sanad"), CurrencyManager)
'cd = CType(Me.BindingContext(DataGridView1.DataSource("sanad_header"), "sanad"), CurrencyManager)
'cd.Refresh()
'cd.Refresh()
con.Close()
con.Close()
结束 如果
End If
End If
End If
Catch ex As Exception
Catch ex As Exception
MsgBox(ex.Message)
MsgBox(ex.Message)
End Try
End Try
End Sub
Only child table display into datagridview1.
How I can referesh datagridview after insert,update and delete operations in child table?
How I can referesh datagridview after insert,update and delete operations in child table?
Can I have one load procedure instead of two (load_sanad_header() and load_sanad_hesabdari()) procedures included navigation operations as follow?
If Not (cd Is Nothing) AndAlso cd.Count Then cd.Position = 0
If Not (cd Is Nothing) AndAlso cd.Count Then cd.Position = 0
If Not (cd Is Nothing) AndAlso cd.Count AndAlso cd.Position < cd.Count - 1 Then cd.Position += 1
If Not (cd Is Nothing) AndAlso cd.Count AndAlso cd.Position < cd.Count - 1 Then cd.Position += 1
If Not (cd Is Nothing) AndAlso cd.Count AndAlso cd.Position > 0 Then cd.Position -= 1
If Not (cd Is Nothing) AndAlso cd.Count AndAlso cd.Position > 0 Then cd.Position -= 1
If Not (cd Is Nothing) AndAlso cd.Count Then cd.Position = cd.Count – 1
If Not (cd Is Nothing) AndAlso cd.Count Then cd.Position = cd.Count – 1
Pls help me.
Best regards.
推荐答案
Why would you need to refresh the grid? You should be making the changes in the grid first. You use a data adapter to Fill a DataTable, you make your changes either via the grid or to the DataTable in code, then you use the same data adapter to Update the database with the changes from the DataTable. The changes affect the grid first so you don't need to do anything after saving the changes to the database.
Why would you need to refresh the grid? You should be making the changes in the grid first. You use a data adapter to Fill a DataTable, you make your changes either via the grid or to the DataTable in code, then you use the same data adapter to Update the database with the changes from the DataTable. The changes affect the grid first so you don't need to do anything after saving the changes to the database.
这篇关于如何在vb.net中的子表中插入,更新和删除数据后如何引用datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!