编辑DataGridView上的单元格

时间:2017-05-01 05:55:19

标签: vb.net datagridview

如何在DataGridView上修改单元格?当我运行项目时,我想编辑单元格,然后单击Button

这是我的代码:

Public Class Form1
    Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=db.accdb")
    'SELECT employ.id, employ.name, employ.adress, employ.phone FROM employ;
    Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        loadAll()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Sub loadAll()
        Dim da As New OleDbDataAdapter("SELECT employ.id, employ.name, employ.adress, employ.phone FROM employ;", con)
        Dim dt As New DataTable()
        da.Fill(dt)
        DataGridView1.Rows.Clear()
        For x = 0 To dt.Rows.Count - 1
            DataGridView1.Rows.Add()
            DataGridView1(0, x).Value = dt.Rows(x)(0).ToString()
            DataGridView1(1, x).Value = dt.Rows(x)(1).ToString()
            DataGridView1(2, x).Value = dt.Rows(x)(2).ToString()
            DataGridView1(3, x).Value = dt.Rows(x)(3).ToString()
        Next

    End Sub

    Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick

        DataGridView1.ReadOnly = False
        DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        DataGridView1.ReadOnly = True
        DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect

        Dim i As Integer

        For i = 0 To DataGridView1.RowCount - 1

            Dim name As String = DataGridView1(1, i).Value.ToString()

        Next

        Dim Str As String = String.Format("update employ set name='{0}' where id= i", Name, DataGridView1(0, DataGridView1.SelectedRows(0).Index).Value.ToString())
        Dim cmd As New OleDbCommand(Str, con)
        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()

    End Sub
End Class

请问这怎么办?

0 个答案:

没有答案