主题:vb2008保存DataGridView到数据库的问题,高手帮一下忙
DataGridView1和 BindingNavigator1都绑定在BindingSource1,按 BindingNavigator1添加记录DataGridView1添加一行,在绑定的文本框里输入数据后,要换一行点击一下表格数据才出现在DataGridView1中,有没有办法让数据输入后就显示出来.
单击保存后数据不能保存到数据库,是哪里写错了,望高手指教一下.
Imports System.Data.SqlClient
Public Class Form1
Public myconn As SqlClient.SqlConnection
Public command1 As New SqlClient.SqlCommand
Public command2 As New SqlClient.SqlCommand
Public myadapter1 As New SqlClient.SqlDataAdapter
Public myadapter2 As New SqlClient.SqlDataAdapter
Public dataset1 As New DataSet
Public dataset2 As New DataSet
Public myReader As SqlDataReader
Public BindingSource1 As New BindingSource
Public BindingSource2 As New BindingSource
Dim builder As SqlCommandBuilder
Private Sub databind()
'用于把控件捆绑数据的过程
DateTimePicker1.DataBindings.Add("text", BindingSource1, "日期")
ComboBox1.DataBindings.Add("text", BindingSource1, "项目")
TextBox1.DataBindings.Add("text", BindingSource1, "金额")
ComboBox2.DataBindings.Add("text", BindingSource1, "经手人")
TextBox2.DataBindings.Add("text", BindingSource1, "备注")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myconn = New SqlClient.SqlConnection '建立连接
myconn.ConnectionString = "Data Source=localhost;Initial Catalog=jtlc;Integrated Security=True"
myconn.Open()
If ConnectionState.Open Then '判断数据库目前的状态是否已打开
' MsgBox("数据库已打开")
command1.Connection = myconn
command1.CommandText = "select * from feiyong"
myadapter1.SelectCommand = command1
dataset1.Tables.Add(New DataTable("feiyong"))
myadapter1.Fill(dataset1, "feiyong")
builder = New SqlCommandBuilder(myadapter1)
BindingSource1.DataSource = dataset1
BindingSource1.DataMember = "feiyong"
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
DataGridView1.MultiSelect = False
DataGridView1.DataSource = BindingSource1 ' dataset1.Tables("feiyong") 'BindingSource1 '绑定DataGridView1
'DataGridView1.DataMember = "feiyong"
BindingNavigator1.BindingSource = BindingSource1 '绑定BindingNavigator1
databind()
'设定DataGridView列宽
DataGridView1.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(3).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(4).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(5).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
'当用户使用DataGridView控件时,它会成为可编辑模式
DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
'bmdata = BindingContext(dataset1, "feiyong") '用BindingContext类的构造函数来获得CurrencyManager对象
command2.Connection = myconn
command2.CommandText = "select * from jsr"
myadapter2.SelectCommand = command2
dataset2.Tables.Add(New DataTable("jsr"))
myadapter2.Fill(dataset2, "jsr") '把从数据源读出的表 jsr的记录数据填充到数据集中,用于提供给ComboBox2选择输入
BindingSource2.DataSource = dataset2.Tables("jsr")
ComboBox2.DataSource = BindingSource2
ComboBox2.DisplayMember = "姓名"
ComboBox2.Text = "" '刚启动窗体时ComboBox2显示为空,不显示绑定的数据
myconn.Close()
End If
ToolStripStatusLabel6.Text = Now()
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
myconn.Open() '保存数据
'BindingSource1.MoveLast()
'builder = New SqlCommandBuilder(myadapter1)
Dim ds As DataSet = CType(dataset1.GetChanges, DataSet)
'builder = New SqlCommandBuilder(myadapter1)
If (Not (ds) Is Nothing) Then
myadapter1.Update(ds.Tables("feiyong"))
dataset1.Merge(ds)
dataset1.AcceptChanges()
End If
'刷新表DataGridView1
command1.CommandText = "select * from feiyong"
myadapter1.SelectCommand = command1
dataset1 = New DataSet
dataset1.Tables.Add(New DataTable("feiyong"))
myadapter1.Fill(dataset1, "feiyong")
'BindingSource1.DataSource = dataset1.Tables("feiyong")
DataGridView1.DataSource = BindingSource1
myconn.Close()
End Sub
End Class
单击保存后数据不能保存到数据库,是哪里写错了,望高手指教一下.
Imports System.Data.SqlClient
Public Class Form1
Public myconn As SqlClient.SqlConnection
Public command1 As New SqlClient.SqlCommand
Public command2 As New SqlClient.SqlCommand
Public myadapter1 As New SqlClient.SqlDataAdapter
Public myadapter2 As New SqlClient.SqlDataAdapter
Public dataset1 As New DataSet
Public dataset2 As New DataSet
Public myReader As SqlDataReader
Public BindingSource1 As New BindingSource
Public BindingSource2 As New BindingSource
Dim builder As SqlCommandBuilder
Private Sub databind()
'用于把控件捆绑数据的过程
DateTimePicker1.DataBindings.Add("text", BindingSource1, "日期")
ComboBox1.DataBindings.Add("text", BindingSource1, "项目")
TextBox1.DataBindings.Add("text", BindingSource1, "金额")
ComboBox2.DataBindings.Add("text", BindingSource1, "经手人")
TextBox2.DataBindings.Add("text", BindingSource1, "备注")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myconn = New SqlClient.SqlConnection '建立连接
myconn.ConnectionString = "Data Source=localhost;Initial Catalog=jtlc;Integrated Security=True"
myconn.Open()
If ConnectionState.Open Then '判断数据库目前的状态是否已打开
' MsgBox("数据库已打开")
command1.Connection = myconn
command1.CommandText = "select * from feiyong"
myadapter1.SelectCommand = command1
dataset1.Tables.Add(New DataTable("feiyong"))
myadapter1.Fill(dataset1, "feiyong")
builder = New SqlCommandBuilder(myadapter1)
BindingSource1.DataSource = dataset1
BindingSource1.DataMember = "feiyong"
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
DataGridView1.MultiSelect = False
DataGridView1.DataSource = BindingSource1 ' dataset1.Tables("feiyong") 'BindingSource1 '绑定DataGridView1
'DataGridView1.DataMember = "feiyong"
BindingNavigator1.BindingSource = BindingSource1 '绑定BindingNavigator1
databind()
'设定DataGridView列宽
DataGridView1.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(3).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(4).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
DataGridView1.Columns(5).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
'当用户使用DataGridView控件时,它会成为可编辑模式
DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
'bmdata = BindingContext(dataset1, "feiyong") '用BindingContext类的构造函数来获得CurrencyManager对象
command2.Connection = myconn
command2.CommandText = "select * from jsr"
myadapter2.SelectCommand = command2
dataset2.Tables.Add(New DataTable("jsr"))
myadapter2.Fill(dataset2, "jsr") '把从数据源读出的表 jsr的记录数据填充到数据集中,用于提供给ComboBox2选择输入
BindingSource2.DataSource = dataset2.Tables("jsr")
ComboBox2.DataSource = BindingSource2
ComboBox2.DisplayMember = "姓名"
ComboBox2.Text = "" '刚启动窗体时ComboBox2显示为空,不显示绑定的数据
myconn.Close()
End If
ToolStripStatusLabel6.Text = Now()
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
myconn.Open() '保存数据
'BindingSource1.MoveLast()
'builder = New SqlCommandBuilder(myadapter1)
Dim ds As DataSet = CType(dataset1.GetChanges, DataSet)
'builder = New SqlCommandBuilder(myadapter1)
If (Not (ds) Is Nothing) Then
myadapter1.Update(ds.Tables("feiyong"))
dataset1.Merge(ds)
dataset1.AcceptChanges()
End If
'刷新表DataGridView1
command1.CommandText = "select * from feiyong"
myadapter1.SelectCommand = command1
dataset1 = New DataSet
dataset1.Tables.Add(New DataTable("feiyong"))
myadapter1.Fill(dataset1, "feiyong")
'BindingSource1.DataSource = dataset1.Tables("feiyong")
DataGridView1.DataSource = BindingSource1
myconn.Close()
End Sub
End Class