当前位置:编程学习 > C#/ASP.NET >>

小弟跪求: 更新DataGridView问题。

我是菜鸟,请各位帮忙解决:我在DataGridView新增数据,直接在单元格中输入,但是不能保存啊。我没有用OracleCommandBuilder,而是用SQL插入,我写了一段小代码请大虾们指点一下。
        private const string SQL_Insert_Student_Info =  @"INSERT INTO Student VALUES(?,?,?)";
        private OracleCommand InsertStudentInfo()
        {
            OracleCommand comm = new SqlCommand(SQL_Insert_Student_Info,this.mconnection);
            OracleParameterCollection StudentPmC = comm.Parameters;
            StudentPmC.Add("Gid_New", SqlDbType.VarChar, 20,"GID");
            StudentPmC.Add("Name_New",SqlDbType.VarChar,20,"Name");
            StudentPmC.Add("Memo_New", SqlDbType.VarChar, 20, "Memo");

            return comm;
        }
        public bool UpdataStudentInfo(DataSet ds)
        {
            try
            {
                OracleDataAdapter sda = new SqlDataAdapter();
                sda.InsertCommand = InsertStudentInfo(); //在此调用前面生成的command对象
                sda.Update(ds, "Student");
                return true;
            }
            catch(Exception ex)
            {
                string strError = ex.Message;
                return false;
            }
        } --------------------编程问答-------------------- 我这个是在里面更新的,你看看能不能用得上,你也可以用这方法添加

private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            DBclass.Db_Class DB = new DBclass.Db_Class();
            try
            {
                
                string priKey = this.dataGridView2["GV_SHOP", e.RowIndex].Value.ToString();
                string up_PROPORTION = this.dataGridView2["GV_PROPORTION", e.RowIndex].Value.ToString();

                string cmdStr = "UPDATE GT_SHOPSCALE SET PROPORTION=" + up_PROPORTION + " WHERE SHOP='" + priKey + "'";

                System.Data.OracleClient.OracleCommand cmd = new OracleCommand(cmdStr, DB.Conn);
                DB.Db_Conn();
               // conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch (OracleException ex)
            {
                MessageBox.Show(ex.Message);
            }
            DB.close();
        }
--------------------编程问答-------------------- 你將代易做图紅色部分改成綠色部分試一試吧.
private const string SQL_Insert_Student_Info =  @"INSERT INTO Student VALUES(?,?,?)"; 
        private OracleCommand InsertStudentInfo() 
        { 
            OracleCommand comm = new SqlCommand(SQL_Insert_Student_Info,this.mconnection); 
            OracleParameterCollection StudentPmC = comm.Parameters; 
            StudentPmC.Add("Gid_New", SqlDbType.VarChar, 20,"GID"); 
            StudentPmC.Add("Name_New",SqlDbType.VarChar,20,"Name"); 
            StudentPmC.Add("Memo_New", SqlDbType.VarChar, 20, "Memo"); 

            return comm; 
        } 
        public bool UpdataStudentInfo(DataSet ds) 
        { 
            try 
            { 
                OracleDataAdapter sda = new SqlDataAdapter(); 
                sda.InsertCommand = InsertStudentInfo(); //在此调用前面生成的command对象 
                sda.Update(ds, "Student"); 
                return true;
                 mconnection.open();      //打開連接;
                 comm.ExecuteNonQuery();
            } 
            catch(Exception ex) 
            { 
                string strError = ex.Message; 
                return false; 
            } 
            mconnection.close();   //關閉連接;
        } --------------------编程问答--------------------
 private void btnUpdate_Click(object sender, EventArgs e)//更新修改项
        {
            try
            {
                bool flag = false;
                OleDbConnection cn = DataBase.CreateAcc();//建立数据库连接,改成你自己的就可以了。
                DataTable dt = (DataTable)dgv.DataSource;//dgv:改成你的datagridview的Name
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i].RowState == DataRowState.Modified)//判断datagridview数据行的状态,是否被修改
                    {
                        OleDbDataAdapter da = new OleDbDataAdapter("select * from Student", cn);
                        OleDbCommandBuilder builder = new OleDbCommandBuilder(da);
                        da.Update(dt);
                        MessageBox.Show("更新成功! ");
                    }
                    else
                    {
                        flag = false;
                    }
                }
                if (!flag)
                {
                    MessageBox.Show("没有进行修改,无法更新");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示");
            }
        }

--------------------编程问答-------------------- 你的oracledataadapter应该是在ds之前就有 --------------------编程问答-------------------- zheninchangejiang 能不能说的清楚些 --------------------编程问答-------------------- 用CommandBuilder对Adapter重新生成命令
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,