在vb.net

时间:2016-02-18 03:57:41

标签: sql database vb.net datagridview datagrid

Private Sub btnAddSub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSub.Click

    Dim comboboxvalue As String
    comboboxvalue = "'" & CBClass.SelectedItem & "'"
    Dim sql As String
    sql = "Select * From class Where ClassCode=" & comboboxvalue
    Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=H:\ProjectDatabase.mdb"
    Dim MyConn As OleDbConnection
    Dim da As OleDbDataAdapter
    Dim ds As DataSet
    Dim tables As DataTableCollection
    Dim source1 As New BindingSource

    MyConn = New OleDbConnection
    MyConn.ConnectionString = connString
    ds = New DataSet
    tables = ds.Tables
    da = New OleDbDataAdapter(sql, MyConn)
    da.Fill(ds, "Class")
    Dim view As New DataView(tables(0))
    source1.DataSource = view
    Form1.dgv.DataSource = view

End Sub

我目前可以将一段数据添加到数据网格中。当我尝试添加第二条数据时,它会替换存储的当前数据。

如何制作它,以便在我添加更多数据时,它会转到新行。

1 个答案:

答案 0 :(得分:0)

您提供的源代码看起来很像您只是在每次btnAddSub点击时刷新DGV的数据源,每次都分配给form1.dgv.datasource。如果您只想将新行添加到DGV,则需要通过向其添加DataGridViewRow对象来操作DGV.Rows集合。