我有一个DataGridView,其中包含以下代码;请注意,代码仅包含重要部分以简洁起见。 gridview填充得很好,但更新代码是我需要帮助的。
Dim da As OleDbDataAdapter
Dim ds As DataSet
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Try
'open database
conn.Open()
'get query data
Dim cmd As OleDbCommand = New OleDbCommand(
"SELECT student_name.matric_no,student_name.sname, student_name.fname," &
"result_sheet.course_title,result_sheet.test_score,result_sheet.exam_score,result_sheet.total_score" &
" FROM student_name INNER JOIN result_sheet ON student_name.matric_no = result_sheet.matric_no ", conn)
da = New OleDbDataAdapter(cmd)
Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(da)
ds = New DataSet()
da.Fill(ds)
da.Dispose()
Dim usertable As DataTable = ds.Tables(0)
'datasource of the datagrid
Dview1.DataSource = usertable
'change the data grid column name
Dview1.Columns(0).HeaderText = "MATRIC NUMBER"
Dview1.Columns(1).HeaderText = "SURNAME"
Dview1.Columns(2).HeaderText = "FIRST NAME"
conn.Close()
Catch ex As Exception
'display message
MsgBox(ex.ToString)
End Try
End Sub
我更新GridView的代码如下;
Private Sub update_btn_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles update_btn.Click
Try
da.Update(ds.Tables(0))
Catch ex As Exception
'display message
MsgBox(ex.ToString)
End Try
End Sub
点击“更新”按钮后出现的错误是:The DataAdapter.SelectCommand property needs to be initialized.
请问,我该如何解决这个问题?