如何将Datagridview中当前选定的行插入数据库

时间:2016-02-03 17:55:52

标签: mysql vb.net winforms datagridview

     For i As Integer = Me.DataGridView1.SelectedRows.Count - 1 To 0 Step -1
            Dim SDA1 As New MySqlDataAdapter
            Dim bSource1 As New BindingSource
            Dim dbDataSet1 As New DataTable
            qty = DataGridView1.Rows(i).Cells(0).Value
            auth = DataGridView1.Rows(i).Cells(1).Value()
            title = DataGridView1.Rows(i).Cells(2).Value()
            callnumber = DataGridView1.Rows(i).Cells(3).Value()
            shelf = DataGridView1.Rows(i).Cells(4).Value()
            Dim Query As String
            Query = "Insert into returnedlist(quantity,author,title ,call_number,shelf,student_id,due_date,date_added)  values('" & qty & "','" & auth & "','" & title & "','" & callnumber & "','" & shelf & "','" & TextBox2.Text.ToString & "','" & DateTimePicker1.Text & "', Now())"
            COMMAND = New MySqlCommand(Query, MysqlConn)
            SDA1.SelectCommand = COMMAND
            SDA1.Fill(dbDataSet1)
            bSource1.DataSource = dbDataSet1
            Me.DataGridView1.DataSource = bSource1
            SDA1.Update(dbDataSet1)
        Next


    **Code for Select**
 ElseIf ComboBox2.Text = "Author" Then
                Dim Query As String
                Query = "Select id as 'ID', quantity as 'Qty',author as 'Author',title as 'Title',call_number as 'Call Number',location as 'Shelf #' from librarydb.blist where author like'%" & TextBox1.Text & "%' ORDER by author"
                COMMAND = New MySqlCommand(Query, MysqlConn)
                SDA.SelectCommand = COMMAND
                If SDA.Fill(dbDataSet) Then
                    bSource.DataSource = dbDataSet
                    DataGridView1.DataSource = bSource
                    TextBox1.Text = ""
                    ComboBox2.Text = ""

                Else
                    TextBox1.Text = ""
                    ComboBox2.Text = ""
                    MessageBox.Show("No Results found", "Informed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If

我想要做的是我想在db中插入我在Datagridview中选择的行。

代码有效但问题是: 即使我选择了row3,它总是会在数据网格视图中插入db的row1。

是否必须对Datagridview的属性进行操作?

1 个答案:

答案 0 :(得分:0)

Update的{​​{1}}方法可以更新作为输入传递的DataTable中存在的所有行,MySqlDataAdapter属性与值RowState不同。

当您编辑绑定到DataTable的DataGridView时,对网格所做的更改会立即反映在基础DataTable中,并且编辑的行将使RowState property相应地更改为编辑。

要完成这项工作,您需要做一些事情。

首先全局声明用于检索数据表的MySqlDataAdapter

Unchanged

然后在加载数据表的代码中,确保还包括作为主键的列

Dim SDA1 As MySqlDataAdapter

现在使用MySqlCommandBuilder自动创建MySqlDataAdapter的InsertCommand,UpdateCommand和DeleteCommand属性

' Here ID is assumed to be your primarykey'
SDA1 = New MySqlDataAdapter("SELECT ID, ...... FROM returnedlist", connection)
SDA1.Fill(yourDataTable)
Dim bSource1 As New BindingSource
bSource1.DataSource = yourDataTable
dataGridView1.DataSource = bSource1

最后,当您准备好更新数据时,只需编写

即可
Dim builder = new MySqlCommandBuilder(SDA1)

网格行上不需要循环并手动构建InsertCommand及其参数

有关如何从DbDataAdapter派生的类如何工作的概述,您可以阅读MSDN reference here