OnEditCommand="DataGrid_Edit" onUpdateCommand="DataGrid_Update"
    Sub DataGrid_Edit(ByVal sender As System.Object, ByVal e As DataGridCommandEventArgs)
        Label2.Text = ""
        Label3.Text = ""
        myDataGrid.EditItemIndex = e.Item.ItemIndex
        strsql = "SELECT id,kind1,kind2,mcon from akind where uname='" & Session("acountuser") & "'"
        BindList()
    End Sub    Sub DataGrid_Cancel(ByVal sender As System.Object, ByVal e As DataGridCommandEventArgs)
        Label2.Text = ""
        Label3.Text = ""
        myDataGrid.EditItemIndex = -1
        strsql = "SELECT id,kind1,kind2,mcon from akind where uname='" & Session("acountuser") & "'"
        BindList()
    End Sub
    Sub DataGrid_Update(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
            Dim sid As Int16 = myDataGrid.DataKeys.Item(e.Item.ItemIndex)
            Dim kind1 As String = e.Item.Cells(0).Text
            Dim kind2 As String = CType(e.Item.Cells(1).Controls(0), TextBox).Text
            Dim mcon As String = CType(e.Item.Cells(2).Controls(1), TextBox).Text                    Dim conn As New OleDbConnection
                    conn.ConnectionString = StrCon
                    conn.Open()
                    strsql = "select * from akind where uname='" & Session("acountuser") & "' and kind1='" & kind1 & "' and kind2='" & kind2 & "' and id<>" & sid & ""
                    Dim strcomm As New OleDbCommand(strsql, conn)
                    Dim dread As OleDbDataReader
                    dread = strcomm.ExecuteReader
                    If dread.HasRows Then
                        Label3.Text = "该小分类已经存在"
                        Label3.ForeColor = Drawing.Color.Red
                        dread.Close()
                        conn.Close()
                    Else
                        dread.Close()
                        If kind1 = "支出" Then
                            strsql = "update akind set kind2='" & kind2 & "',mcon=" & mcon & " where id=" & sid & ""
                        Else
                            strsql = "update akind set kind2='" & kind2 & "' where id=" & sid & ""
                        End If
                        strcomm.CommandText = strsql
                        strcomm.ExecuteNonQuery()
                        conn.Close()
                        myDataGrid.EditItemIndex = -1
                        strsql = "SELECT id,kind1,kind2,mcon from akind where uname='" & Session("acountuser") & "'"
                        BindList()
                        Label3.Text = "更新成功"
                        Label3.ForeColor = Drawing.Color.Red
                    End If    End Sub

解决方案 »

  1.   

    我已经把表信息显示出来了,
    private void button5_Click(object sender, System.EventArgs e)
    {
                                 }
    里面有写什么代码
      

  2.   

    要写edit和update事件啊
    认真看看datagrid的教材吧
      

  3.   

    信息都显示出来了,要写到另外一个表中,直接写一句sql语句执行就可以。
      

  4.   

    点击datagridview1中的某一行生,下面的文本框中显示选中的记录!
    private void dataGridView1_CurrentCellChanged(object sender, System.EventArgs e)
            {            try
                {
                    NowID = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误信息:"+ex.Message );
                }
                    if (NowID != "")
                    {
                        SqlDataReader sdr = Function.SqlReader("select top 1 * from bq_note where ID=" + NowID, Function.connStr);
                        if (sdr != null)
                        {
                            if (sdr.Read())
                            {
                                textBox1.Text = sdr["note_body"].ToString();
                                textBox2.Text = sdr["note_name"].ToString();
                                textBox3.Text = sdr["note_date"].ToString();
                            }
                            sdr.Close();
                        }
                    }
            }