用一个datagrid显示表table_user的内容.然后有三个按钮,分别是插入修改删除,单击按钮实现功能不会了,帮帮忙吧我是一个初学者

解决方案 »

  1.   

    你能不能说出是网页的还是windows应用程序?
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.OracleClient;namespace test
    {
        public partial class BaseForm : Form
        {
            public string strConn ;
            public string strSql ;
            public string tableName ;
            public string[] colName;
            public BaseForm()
            {
                InitializeComponent();
                
            }
            
            public void Connect( )
            {
                OracleConnection conn = new OracleConnection(strConn);
               
                OracleDataAdapter da = new OracleDataAdapter(strSql, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, tableName);
                this.bindingSource1.DataSource = ds;
                this.bindingSource1.DataMember = tableName;
                this.dataGridView1.DataSource = this.bindingSource1;        }
            public void Del(string aa)
            {            OracleConnection conn = new OracleConnection(strConn);
                conn.Open();
                string delSql = "delete " + tableName + " where id =:" + colName[0];
                OracleCommand cmd = new OracleCommand(delSql, conn);            cmd.Parameters.Add("id",aa);            cmd.ExecuteNonQuery();
                conn.Close();
            }
            public void Add(string[] col)
            {
                OracleConnection conn = new OracleConnection(strConn);
                conn.Open();
                string addSql = "insert into " + tableName + " values(" + "'" + col[0] + "'" + "," + "'" + col[1] + "'" + "," + "'" + col[2] + "'" + ")";
                OracleCommand cmd = new OracleCommand(addSql,conn);
                cmd.ExecuteNonQuery();
            }        public void Edit(string[] col)
            {
                OracleConnection conn = new OracleConnection(strConn);
                conn.Open();
                string editSql = "update " + tableName + " set " + colName[1] + "='" + col[1] + "' , " + colName[2]  +"='" + col[2] + "'" + " where " + colName[0] + "=" + "'"+col[0]+"'";
                OracleCommand cmd = new OracleCommand(editSql, conn);
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }        public void button1_Click(object sender, EventArgs e)
            {            if (this.dataGridView1.CurrentRow.Index  == 0)
                {
                    MessageBox.Show("已经是第一行了!");
                    //this.button1.Enabled = false;
                }
                else
                {
                    //this.button1.Enabled = true;
                    this.bindingSource1.MovePrevious();
                }
            }        public void button2_Click(object sender, EventArgs e)
            {
                if (this.dataGridView1.CurrentRow.Index >= this.dataGridView1.RowCount - 2)
                {
                    //this.button2.Enabled = false;                MessageBox.Show("已经是最后一行了!");
                }
                else
                {
                    //this.button2.Enabled = true;                this.bindingSource1.MoveNext();
                }
               
            }        public void button3_Click(object sender, EventArgs e)
            {
                string aa;
                try
                {                aa = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
                   
                }
                catch (Exception a)
                {
                    return;
                }            Del(aa);
                
                Connect();
                MessageBox.Show("删除成功!");
             
            }        private void button4_Click(object sender, EventArgs e)
            {
                this.bindingSource1.AddNew();
            }        private void button5_Click(object sender, EventArgs e)
            {
                try
                {
                    string[] col = new string[] { this.dataGridView1.CurrentRow.Cells[0].Value.ToString(), this.dataGridView1.CurrentRow.Cells[1].Value.ToString(), this.dataGridView1.CurrentRow.Cells[2].Value.ToString() };
                    Add(col);
                    MessageBox.Show("保存成功!");
                }
                catch (Exception a)
                {
                    MessageBox.Show("保存失败!");
                }
                
            }        private void button6_Click(object sender, EventArgs e)
            {
                try
                {
                    string[] col = new string[] { this.dataGridView1.CurrentRow.Cells[0].Value.ToString(), this.dataGridView1.CurrentRow.Cells[1].Value.ToString(), this.dataGridView1.CurrentRow.Cells[2].Value.ToString() };
                    Edit(col);
                    MessageBox.Show("修改成功!");
                }
                catch (Exception a)
                {
                    MessageBox.Show("修改失败!");
                }
            }    }
    }
      

  3.   

    .NET中编辑数据竟然这么麻烦啊!
    有什么比较简单的方法,类似以前的DELPHI?
      

  4.   

    this.bindingSource1.DataSource   =   ds; 
    为什么我的不出现这个this.后面没有bindingsource
    我是vs2003
      

  5.   

     DataSet ds;
            System.Data.OleDb.OleDbDataAdapter da;
            private static string ConnStr = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Application.StartupPath + "\\" + "BoBaiData.mdb";
            public static System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(ConnStr);
            private void Form3_Load(object sender, EventArgs e)
            {
                string sql="select * from DanWei";
                da=new System.Data.OleDb.OleDbDataAdapter (sql, conn);
                System.Data.OleDb.OleDbCommandBuilder cb = new System.Data.OleDb.OleDbCommandBuilder(da);
                 ds = new DataSet();
                da.Fill(ds,"co");
                this.dataGridView1.DataSource = ds;
                this.dataGridView1.DataMember = "co";        }               private void button1_Click(object sender, EventArgs e)
            {
                da.Update(ds, "co");
            }
      

  6.   

    private void button2_Click(object sender, EventArgs e)
            {
               DataSet dsAdd=ds.GetChanges(DataRowState.Added);
               da.Update(dsAdd,"co");
            }        private void button3_Click(object sender, EventArgs e)
            {
                DataSet dsUpe = ds.GetChanges(DataRowState.Modified);
                da.Update(dsUpe,"co");
            }        private void button4_Click(object sender, EventArgs e)
            {
                DataSet dsDel = ds.GetChanges(DataRowState.Deleted);
                da.Update(dsDel,"co");
            
            }
      

  7.   

    用一个datagrid显示表table_user的内容.然后有三个按钮,分别是插入修改删除,单击按钮实现功能不会了,帮帮忙吧我是一个初学者
    其实不用这么麻烦!DataTable dataTable = new DataTable(); 
    this.sqlConnection = sqlConnection;
    sqlCommand = new SqlCommand(selectStr, this.sqlConnection );
    this.sqlDataAdapter = new SqlDataAdapter(this.sqlCommand);
    this.sqlDataAdapter.Fill(dataTable);
    this.dataGridView1.DataSource=dataTable;
    //加入数据
    DataRow NewRow=dataTable.newRow()
    NewRow["nameFeild"]=""
    .....
    dataTable.rows.add(newRow)
    //删除数据
    datatable..Rows.RemoveAt(this.dataGridView1.CurrentRow .Index );