单击按钮时如何编辑选定的行?

时间:2017-02-25 21:40:06

标签: vb.net datagridview

我试图编辑datagridview中所选行的数据,如果单击该按钮并将其放在文本框中以更新数据,那么如果我选择一行直接将其放入,则会发生什么?单击编辑按钮之前的文本框,但我想要的是如果我选择一行然后单击编辑按钮,所选行中的数据将被放在提供的文本框中。有谁可以帮助我吗.. 这是我在datagridview中的代码:

index = e.RowIndex Dim selectedRow As DataGridViewRow selectedRow = DataGridView1.Rows(index) TextBox8.Text = selectedRow.Cells(1).Value.ToString TextBox1.Text = selectedRow.Cells(2).Value.ToString TextBox2.Text = selectedRow.Cells(3).Value.ToString TextBox3.Text = selectedRow.Cells(4).Value.ToString TextBox4.Text = selectedRow.Cells(5).Value.ToString TextBox5.Text = selectedRow.Cells(6).Value.ToString TextBox6.Text = selectedRow.Cells(7).Value.ToString ComboBox1.Text = selectedRow.Cells(8).Value.ToString TextBox9.Text = selectedRow.Cells(9).Value.ToString

然后这是我在编辑按钮中的代码

Try
        Dim cmd As New MySqlCommand
        connect.Open()
        cmd.Connection = connect
        da = New MySqlDataAdapter("Select * from tbl_book_info", connect)
        cmd.CommandText = "update tbl_book_info set (Title = '" & TextBox1.Text & "',Author = '" & TextBox2.Text & "',Copyright = '" & TextBox3.Text & "',Publisher = '" & TextBox4.Text & "',Place = '" & TextBox5.Text & "',Copies = '" & TextBox6.Text & "', Subject = '" & ComboBox1.Text & "', ClassNo = '" & TextBox8.Text & "',Registered by = '" & TextBox9.Text & "',Time = '" & TextBox10.Text & "',Date = '" & TextBox11.Text & "')"
        cmd.ExecuteNonQuery()
        MsgBox("Already Updated!")

        MessageBox.Show("update tbl_book_info set (Title = '" & TextBox1.Text & "',Author = '" & TextBox2.Text & "',Copyright = '" & TextBox3.Text & "',Publisher = '" & TextBox4.Text & "',Place = '" & TextBox5.Text & "',Copies = '" & TextBox6.Text & "', Subject = '" & ComboBox1.Text & "', ClassNo = '" & TextBox8.Text & "',Registered by = '" & TextBox9.Text & "',Time = '" & TextBox10.Text & "',Date = '" & TextBox11.Text & "')")
        data = New DataTable()
        DataGridView1.DataSource = data

        da = New MySqlDataAdapter("Select * from tbl_book_info", connect)
        da.Fill(data)

    Catch ex As Exception

    End Try

1 个答案:

答案 0 :(得分:0)

enter image description here

Dim x As Integer
Dim y As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    TextBox1.Text = DataGridView1.SelectedCells(0).Value()
    x = DataGridView1.SelectedCells(0).RowIndex
    y = DataGridView1.CurrentCell.ColumnIndex
    DataGridView1.Enabled = False
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    DataGridView1.Enabled = True
    DataGridView1.Rows(x).Cells(y).Value = TextBox1.Text
End Sub