更新数据网格视图?
Updating Datagridview?
我想知道如何更新我的 DataGridView 中的 selected 行。我希望我的系统能够识别我从我的数据网格视图中 select 编辑的现有信息行(顺便说一句,我已将其设置为整行 select)和 update/edit 通过更改信息它们与我用来添加它们的文本框和组合框。我的 table 中的所有内容都设置为文本数据类型,除了 ID 是自动编号。但是,我在下面使用的代码中遇到错误。任何帮助将不胜感激!谢谢 :)(由于我没有足够的声誉,我将在错误的屏幕截图中提供 link。)
* 当我用方括号括起部分时出现新错误
http://i.imgur.com/gs8rVhB.png
Imports System.Data.OleDb
Public Class AdmMain
Dim con As New OleDbConnection
Sub fillcombo()
strsql = " select yrgr from yearandgrade"
Dim acscmd As New OleDb.OleDbCommand
acscmd.CommandText = strsql
acscmd.Connection = acsconn
acsdr = acscmd.ExecuteReader
While (acsdr.Read())
cboyr.Items.Add(acsdr("yrgr"))
End While
acscmd.Dispose()
acsdr.Close()
End Sub
Sub comb2()
strsql = " select sections from sectio"
Dim acscmd As New OleDb.OleDbCommand
acscmd.CommandText = strsql
acscmd.Connection = acsconn
acsdr = acscmd.ExecuteReader
While (acsdr.Read())
cbosec.Items.Add(acsdr("sections"))
End While
acscmd.Dispose()
acsdr.Close()
End Sub
Private Sub LinkLabel1_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
If MessageBox.Show("Are you sure you want to logout?", "Logout", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
MessageBox.Show("You have successfully logged out of VCM's Library Information System!", "Logout Confirmed")
Me.Close()
LoginUser.Show()
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Me.TxtID.Text = ""
Me.txtFName.Text = ""
Me.txtMName.Text = ""
Me.txtLName.Text = ""
Me.cboyr.Text = ""
Me.cbosec.Text = ""
Me.TxtID.Focus()
End Sub
Private Sub AdmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Module1.connect()
Me.fillcombo()
Me.comb2()
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Thesis\Thesis\Database1.accdb"
con.Open()
datagridshow()
End Sub
Private Sub datagridshow()
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
da = New OleDbDataAdapter("select * from students ", con)
da.Fill(dt)
DataGridView1.DataSource = dt.DefaultView
con.Close()
End Sub
Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
Dim rbdtext As String = cboyr.SelectedItem.ToString
Dim uno As String = cbosec.SelectedItem.ToString
Try
Dim cnString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=C:\Thesis\Thesis\Database1.accdb"
Dim sqlquery As String = "INSERT INTO students " & _
"(StudentID, FirstName,MiddleName,LastName,Yr, [Section]) " & _
"VALUES (@studid, @fname,@mname,@lname,@yr, @sec)"
' Use this form to initialize both connection and command to
' avoid forgetting to set the appropriate properties....
Using conn = New System.Data.OleDb.OleDbConnection(cnString)
Using cmd = New System.Data.OleDb.OleDbCommand(sqlquery, conn)
conn.Open()
cmd.Parameters.AddWithValue("@studid", TxtID.Text)
cmd.Parameters.AddWithValue("@fname", txtFName.Text)
cmd.Parameters.AddWithValue("@mname", txtMName.Text)
cmd.Parameters.AddWithValue("@lname", txtLName.Text)
cmd.Parameters.AddWithValue("@yr", rbdtext)
cmd.Parameters.AddWithValue("@sec", uno)
If TxtID.Text = "" Or txtFName.Text = "" Or txtMName.Text = "" Or txtLName.Text = "" Or cboyr.SelectedIndex = -1 Or cbosec.SelectedIndex = -1 Then
MessageBox.Show("Please complete the required fields.", "Admin", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return
Else
Dim rowsInserted = cmd.ExecuteNonQuery()
If rowsInserted > 0 Then
MessageBox.Show("One record successfully added!", "Added!")
datagridshow()
Else
MessageBox.Show("Failure to add new record!", "Failure!")
End If
End If
End Using
End Using
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub TxtID_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TxtID.KeyPress
'97 - 122 = Ascii codes for simple letters
'65 - 90 = Ascii codes for capital letters
'48 - 57 = Ascii codes for numbers
If Asc(e.KeyChar) <> 8 Then
If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
e.Handled = True
End If
End If
End Sub
Private Sub btnEdit_Click(sender As System.Object, e As System.EventArgs) Handles btnEdit.Click
Dim rbdtext As String = cboyr.SelectedItem.ToString
Dim uno As String = cbosec.SelectedItem.ToString
Try
Dim cnString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=C:\Thesis\Thesis\Database1.accdb"
Dim sqlquery As String = "UPDATE Students SET StudentID = @STUDID, FirstName = @FNAME, MiddleName = @MNAME, LastName= @LNAME, Yr = @YRR, [Section] = @SEC WHERE StudentID = @STUDID, FirstName =@FNAME, MiddleName = @MNAME, LastName= @LNAME, Yr = @YRR, [Section] = @SEC"
' Use this form to initialize both connection and command to
' avoid forgetting to set the appropriate properties....
Using conn = New System.Data.OleDb.OleDbConnection(cnString)
Using cmd = New System.Data.OleDb.OleDbCommand(sqlquery, conn)
conn.Open()
cmd.Parameters.AddWithValue("@STUDID", TxtID.Text)
cmd.Parameters.AddWithValue("@FNAME", txtFName.Text)
cmd.Parameters.AddWithValue("@MNAME", txtMName.Text)
cmd.Parameters.AddWithValue("@LNAME", txtLName.Text)
cmd.Parameters.AddWithValue("@YRR", rbdtext)
cmd.Parameters.AddWithValue("@SEC", uno)
If TxtID.Text = "" Or txtFName.Text = "" Or txtMName.Text = "" Or txtLName.Text = "" Or cboyr.SelectedIndex = -1 Or cbosec.SelectedIndex = -1 Then
MessageBox.Show("Please complete the required fields.", "Admin", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return
Else
Dim rowsInserted = cmd.ExecuteNonQuery()
If rowsInserted > 0 Then
MessageBox.Show("One record successfully updated!", "Updated!")
datagridshow()
Else
MessageBox.Show("Failure to update new record!", "Failure!")
End If
End If
End Using
End Using
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
UPDATE...WHERE 表达式的语法需要一个唯一标识要更新的记录的条件。如果条件是由多个 field/value 组成的表达式,则应使用不使用逗号的 AND 或 OR 运算符组合这些条件。
通常 WHERE 子句中的条件使用 table 的 PRIMARY KEY 标识要更新的记录(在您的情况下,这似乎是 StudentID 字段),因此您只需要编写
Dim sqlquery As String = "UPDATE Students SET FirstName = @FNAME, " &_
"MiddleName = @MNAME, LastName= @LNAME, Yr = @YRR, " & _
"[Section] = @SEC " & _
"WHERE StudentID = @STUDID"
请注意,如果 StudentID 是 PRIMARYKEY,您不要尝试 change/update 它因为这可能会导致您的其他数据库 table 与学生 table 引用(有关系)table
最后,OleDb 提供程序无法通过名称识别参数,提供程序希望传递的每个参数的顺序与参数占位符在命令文本中出现的顺序相同(它使用参数的位置将值传递给数据库引擎),因此您还需要更改为 UPDATE
设置参数集合的顺序
cmd.Parameters.AddWithValue("@FNAME", txtFName.Text)
cmd.Parameters.AddWithValue("@MNAME", txtMName.Text)
cmd.Parameters.AddWithValue("@LNAME", txtLName.Text)
cmd.Parameters.AddWithValue("@YRR", rbdtext)
cmd.Parameters.AddWithValue("@SEC", uno)
cmd.Parameters.AddWithValue("@STUDID", TxtID.Text)
我想知道如何更新我的 DataGridView 中的 selected 行。我希望我的系统能够识别我从我的数据网格视图中 select 编辑的现有信息行(顺便说一句,我已将其设置为整行 select)和 update/edit 通过更改信息它们与我用来添加它们的文本框和组合框。我的 table 中的所有内容都设置为文本数据类型,除了 ID 是自动编号。但是,我在下面使用的代码中遇到错误。任何帮助将不胜感激!谢谢 :)(由于我没有足够的声誉,我将在错误的屏幕截图中提供 link。) * 当我用方括号括起部分时出现新错误 http://i.imgur.com/gs8rVhB.png
Imports System.Data.OleDb
Public Class AdmMain
Dim con As New OleDbConnection
Sub fillcombo()
strsql = " select yrgr from yearandgrade"
Dim acscmd As New OleDb.OleDbCommand
acscmd.CommandText = strsql
acscmd.Connection = acsconn
acsdr = acscmd.ExecuteReader
While (acsdr.Read())
cboyr.Items.Add(acsdr("yrgr"))
End While
acscmd.Dispose()
acsdr.Close()
End Sub
Sub comb2()
strsql = " select sections from sectio"
Dim acscmd As New OleDb.OleDbCommand
acscmd.CommandText = strsql
acscmd.Connection = acsconn
acsdr = acscmd.ExecuteReader
While (acsdr.Read())
cbosec.Items.Add(acsdr("sections"))
End While
acscmd.Dispose()
acsdr.Close()
End Sub
Private Sub LinkLabel1_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
If MessageBox.Show("Are you sure you want to logout?", "Logout", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
MessageBox.Show("You have successfully logged out of VCM's Library Information System!", "Logout Confirmed")
Me.Close()
LoginUser.Show()
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Me.TxtID.Text = ""
Me.txtFName.Text = ""
Me.txtMName.Text = ""
Me.txtLName.Text = ""
Me.cboyr.Text = ""
Me.cbosec.Text = ""
Me.TxtID.Focus()
End Sub
Private Sub AdmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Module1.connect()
Me.fillcombo()
Me.comb2()
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Thesis\Thesis\Database1.accdb"
con.Open()
datagridshow()
End Sub
Private Sub datagridshow()
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
da = New OleDbDataAdapter("select * from students ", con)
da.Fill(dt)
DataGridView1.DataSource = dt.DefaultView
con.Close()
End Sub
Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
Dim rbdtext As String = cboyr.SelectedItem.ToString
Dim uno As String = cbosec.SelectedItem.ToString
Try
Dim cnString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=C:\Thesis\Thesis\Database1.accdb"
Dim sqlquery As String = "INSERT INTO students " & _
"(StudentID, FirstName,MiddleName,LastName,Yr, [Section]) " & _
"VALUES (@studid, @fname,@mname,@lname,@yr, @sec)"
' Use this form to initialize both connection and command to
' avoid forgetting to set the appropriate properties....
Using conn = New System.Data.OleDb.OleDbConnection(cnString)
Using cmd = New System.Data.OleDb.OleDbCommand(sqlquery, conn)
conn.Open()
cmd.Parameters.AddWithValue("@studid", TxtID.Text)
cmd.Parameters.AddWithValue("@fname", txtFName.Text)
cmd.Parameters.AddWithValue("@mname", txtMName.Text)
cmd.Parameters.AddWithValue("@lname", txtLName.Text)
cmd.Parameters.AddWithValue("@yr", rbdtext)
cmd.Parameters.AddWithValue("@sec", uno)
If TxtID.Text = "" Or txtFName.Text = "" Or txtMName.Text = "" Or txtLName.Text = "" Or cboyr.SelectedIndex = -1 Or cbosec.SelectedIndex = -1 Then
MessageBox.Show("Please complete the required fields.", "Admin", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return
Else
Dim rowsInserted = cmd.ExecuteNonQuery()
If rowsInserted > 0 Then
MessageBox.Show("One record successfully added!", "Added!")
datagridshow()
Else
MessageBox.Show("Failure to add new record!", "Failure!")
End If
End If
End Using
End Using
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub TxtID_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TxtID.KeyPress
'97 - 122 = Ascii codes for simple letters
'65 - 90 = Ascii codes for capital letters
'48 - 57 = Ascii codes for numbers
If Asc(e.KeyChar) <> 8 Then
If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
e.Handled = True
End If
End If
End Sub
Private Sub btnEdit_Click(sender As System.Object, e As System.EventArgs) Handles btnEdit.Click
Dim rbdtext As String = cboyr.SelectedItem.ToString
Dim uno As String = cbosec.SelectedItem.ToString
Try
Dim cnString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=C:\Thesis\Thesis\Database1.accdb"
Dim sqlquery As String = "UPDATE Students SET StudentID = @STUDID, FirstName = @FNAME, MiddleName = @MNAME, LastName= @LNAME, Yr = @YRR, [Section] = @SEC WHERE StudentID = @STUDID, FirstName =@FNAME, MiddleName = @MNAME, LastName= @LNAME, Yr = @YRR, [Section] = @SEC"
' Use this form to initialize both connection and command to
' avoid forgetting to set the appropriate properties....
Using conn = New System.Data.OleDb.OleDbConnection(cnString)
Using cmd = New System.Data.OleDb.OleDbCommand(sqlquery, conn)
conn.Open()
cmd.Parameters.AddWithValue("@STUDID", TxtID.Text)
cmd.Parameters.AddWithValue("@FNAME", txtFName.Text)
cmd.Parameters.AddWithValue("@MNAME", txtMName.Text)
cmd.Parameters.AddWithValue("@LNAME", txtLName.Text)
cmd.Parameters.AddWithValue("@YRR", rbdtext)
cmd.Parameters.AddWithValue("@SEC", uno)
If TxtID.Text = "" Or txtFName.Text = "" Or txtMName.Text = "" Or txtLName.Text = "" Or cboyr.SelectedIndex = -1 Or cbosec.SelectedIndex = -1 Then
MessageBox.Show("Please complete the required fields.", "Admin", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return
Else
Dim rowsInserted = cmd.ExecuteNonQuery()
If rowsInserted > 0 Then
MessageBox.Show("One record successfully updated!", "Updated!")
datagridshow()
Else
MessageBox.Show("Failure to update new record!", "Failure!")
End If
End If
End Using
End Using
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
UPDATE...WHERE 表达式的语法需要一个唯一标识要更新的记录的条件。如果条件是由多个 field/value 组成的表达式,则应使用不使用逗号的 AND 或 OR 运算符组合这些条件。
通常 WHERE 子句中的条件使用 table 的 PRIMARY KEY 标识要更新的记录(在您的情况下,这似乎是 StudentID 字段),因此您只需要编写
Dim sqlquery As String = "UPDATE Students SET FirstName = @FNAME, " &_
"MiddleName = @MNAME, LastName= @LNAME, Yr = @YRR, " & _
"[Section] = @SEC " & _
"WHERE StudentID = @STUDID"
请注意,如果 StudentID 是 PRIMARYKEY,您不要尝试 change/update 它因为这可能会导致您的其他数据库 table 与学生 table 引用(有关系)table
最后,OleDb 提供程序无法通过名称识别参数,提供程序希望传递的每个参数的顺序与参数占位符在命令文本中出现的顺序相同(它使用参数的位置将值传递给数据库引擎),因此您还需要更改为 UPDATE
设置参数集合的顺序cmd.Parameters.AddWithValue("@FNAME", txtFName.Text)
cmd.Parameters.AddWithValue("@MNAME", txtMName.Text)
cmd.Parameters.AddWithValue("@LNAME", txtLName.Text)
cmd.Parameters.AddWithValue("@YRR", rbdtext)
cmd.Parameters.AddWithValue("@SEC", uno)
cmd.Parameters.AddWithValue("@STUDID", TxtID.Text)