VB.Net:添加/更新/删除

时间:2017-04-24 07:23:57

标签: vb.net datagridview combobox

我有两种形式: MainForm.vb AddForm.vb

AddForm.vb 将记录添加到数据库中。
MainForm.vb 基本上有 DataGridView 更新 < em>删除 功能。

在MainForm.vb中,我有一个组合框。组合框将全名存储在数据库中。这是加载功能

中的代码
Dim con As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Lanz Buyao\Documents\Visual Studio 2015\Projects\OffenseDatabase\OffenseDatabase\dbOffense.mdf;Integrated Security=True")

    Me.TblStudentTableAdapter3.Fill(Me.DbOffenseDataSet5.tblStudent)
    Me.TblOffenseTableAdapter3.Fill(Me.DbOffenseDataSet5.tblOffense)
    Me.TblGetsTableAdapter3.Fill(Me.DbOffenseDataSet5.tblGets)

    Dim adp As New SqlDataAdapter("SELECT Student_ID, FullName FROM tblStudent ORDER BY LastName", con)
    Dim tbl As New DataTable

    adp.Fill(tbl)

    adp.Dispose()
    con.Dispose()

    ComboBox1.DisplayMember = "FullName"
    ComboBox1.ValueMember = "Student_ID"
    ComboBox1.DataSource = tbl


它可以正常工作。它正确地将数据显示到组合框

这是组合框的事件代码

Try
        con.Open()

        Dim CTR As Integer
        Dim das As New SqlCommand("SELECT COUNT (tblGets.Student_ID) As CTR FROM tblGets INNER JOIN tblStudent on tblStudent.Student_ID = tblGets.Student_ID where tblGets.Student_ID = '" & ComboBox1.SelectedValue.ToString() & "' AND tblStudent.Section = '" & hiddentextbox.Text & "'", con)

        CTR = das.ExecuteScalar()
        das = Nothing

        Dim ds As New DataSet
        Dim dt As New DataTable
        ds.Tables.Add(dt)
        Dim da As New SqlDataAdapter



        'display in table
        da = New SqlDataAdapter("Select tblGets.Student_ID, tblStudent.LastName, tblStudent.FirstName, tblStudent.MiddleName, tblStudent.Gender, tblStudent.ContactNumber, tblGets.OffenseCode, tblOffense.OffenseDesc,
                     tblOffense.OffenseCategory, tblGets.Date, tblStudent.Remarks, tblStudent.Guardian, tblStudent.Location, tblStudent.Address FROM tblStudent INNER JOIN
                     tblGets ON tblStudent.Student_ID = tblGets.Student_ID INNER JOIN
                     tblOffense ON tblGets.OffenseCode = tblOffense.OffenseCode WHERE (tblStudent.Student_ID = '" & ComboBox1.SelectedValue.ToString() & "' AND tblStudent.Section = '" & hiddentextbox.Text & "')", con)

        da.Fill(dt)
        DataGridView1.DataSource = dt.DefaultView
        con.Close()

        'display in textbox
        If ds.Tables(0).Rows.Count > 0 Then
            editlastname.Text = ds.Tables(0).Rows(0)("LastName").ToString()
            editfirstname.Text = ds.Tables(0).Rows(0)("FirstName").ToString()
            editmiddlename.Text = ds.Tables(0).Rows(0)("MiddleName").ToString()
            editcontact.Text = ds.Tables(0).Rows(0)("ContactNumber").ToString()
            grdn.Text = ds.Tables(0).Rows(0)("Guardian").ToString()
            loc.Text = ds.Tables(0).Rows(0)("Location").ToString()
            address.Text = ds.Tables(0).Rows(0)("Address").ToString()

        Else
            hdlbl.Text = "changed"
            editlastname.Clear()
            editfirstname.Clear()
            editmiddlename.Clear()
            editcontact.Clear()
            grdn.Clear()
            loc.Clear()
            address.Text = ""

        End If

        If CTR = 0 Then
            STATUS.Text = "NONE"
        ElseIf CTR = 1 Then
            STATUS.Text = "WARNING"
        ElseIf CTR = 2 Then
            STATUS.Text = "CALL PARENT"
        ElseIf CTR = 3 Then
            STATUS.Text = "SUSPENSION"
        ElseIf CTR = 4 Then
            STATUS.Text = "SUSPENSION"
        End If

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

    If ComboBox1.Text = "" Then
        con.Open()
        Dim ds As New DataSet
        Dim dt As New DataTable
        ds.Tables.Add(dt)
        Dim da As New SqlDataAdapter
        da = New SqlDataAdapter("SELECT tblGets.Student_ID,tblStudent.LastName, tblStudent.FirstName, tblStudent.MiddleName, tblGets.OffenseCode,  tblOffense.OffenseDesc, tblOffense.OffenseCategory, tblGets.Date FROM tblGets INNER JOIN
                     tblOffense ON tblGets.OffenseCode = tblOffense.OffenseCode INNER JOIN
                     tblStudent ON tblGets.Student_ID = tblStudent.Student_ID WHERE tblStudent.Section = '" & hiddentextbox.Text & "' ORDER BY tblStudent.LastName", con)
        da.Fill(dt)

        DataGridView1.DataSource = dt.DefaultView
        con.Close()
    End If

此代码可以实现应有的功能。它正确显示我在DataGridView上单击的项目的内容。

现在,问题出现了。

我有一个刷新按钮来测试一切是否令人耳目一新。显然,我尝试过的解决方案都没有。

解决方案1 ​​ 再次调用MainForm_Load函数。像这样... MainForm_Load(Nothing, Nothing)因为它与此冲突,所以不能正常工作。 Me.TblStudentTableAdapter3.Fill(Me.DbOffenseDataSet5.tblStudent) Me.TblOffenseTableAdapter3.Fill(Me.DbOffenseDataSet5.tblOffense) Me.TblGetsTableAdapter3.Fill(Me.DbOffenseDataSet5.tblGets)

解决方案2 首先将ComboBox1.DataSource声明为Nothing,然后使用在MainForm_Load函数中绑定的相同代码行重新填充它。每当我尝试这样做时,它显然与ComboBox1.ValueMember.ToString()冲突。

我在网上搜索了一些参考资料,他们都说了同样的话。有些是如此复杂,以至于它不适用于我的代码。我已经在这五天了,这是我唯一的问题。

所以,如果有人可以提供帮助,这会让一些正在努力解决同一问题的人受到青睐。

0 个答案:

没有答案