简体   繁体   中英

Adding more rows to a datagridview in vb.net

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

I can currently add one piece of data to the datagrid. When I try to add a second piece of data it replaces the current data stored.

How do I make it so that when I add more data it goes to a new line.

The source code you provided looks a lot like you are just refreshing the data source for the DGV on every btnAddSub click, by assigning to form1.dgv.datasource each time. If you want to simply append new rows to the DGV you will need to manipulate the DGV.Rows collection by adding DataGridViewRow objects to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM