2 kawun kawun 于 2016.09.07 14:16 提问

c# dataAdapter 修改数据源 2C

我想从数据源取东西放在datagridview上,通过datagridview的增删改再改变数据源。代码写出来,实现不了,请大家帮忙看下。最好是通过两个按钮来实现,一个查询,一个确定修改.
代码如下:

    string rateSql = string.Format(@"select r_time_int,r_time_unit,r_rate from rate where r_p_id = {0}", productId);
    OleDbCommand rateSqlCmd = new OleDbCommand(rateSql, DBHelper.conn);
    OleDbDataAdapter rateSqlAdp = new OleDbDataAdapter(rateSqlCmd);
    ds = new DataSet();
    rateSqlAdp.Fill(ds);
    DataTable dt = ds.Tables[0];
    OleDbCommandBuilder cmdb = new OleDbCommandBuilder(rateSqlAdp);
    rateSqlAdp.Update(ds);
    dataGridView1.DataSource = dt;

2个回答

kawun
kawun   2016.09.07 14:22

忘了贴代码了。。。
string rateSql = string.Format(@"select r_time_int,r_time_unit,r_rate from rate where r_p_id = {0}", productId);
OleDbCommand rateSqlCmd = new OleDbCommand(rateSql, DBHelper.conn);
OleDbDataAdapter rateSqlAdp = new OleDbDataAdapter(rateSqlCmd);
ds = new DataSet();
rateSqlAdp.Fill(ds);
DataTable dt = ds.Tables[0];
OleDbCommandBuilder cmdb = new OleDbCommandBuilder(rateSqlAdp);
rateSqlAdp.Update(ds);
dataGridView1.DataSource = dt;

qq_34418970
qq_34418970   2016.09.07 16:05

protected void btnSelect_Click(object sender, EventArgs e)
{
string sql = "select .............";
OleDbCommand rateSqlCmd = new OleDbCommand(sql, DBHelper.conn);
OleDbDataAdapter rateSqlAdp = new OleDbDataAdapter(rateSqlCmd);
DataSet ds = new DataSet();
rateSqlAdp.Fill(ds);
DataTable dt = ds.Tables[0];
OleDbCommandBuilder cmdb = new OleDbCommandBuilder(rateSqlAdp);
rateSqlAdp.Update(ds);
GridView1.DataSource = dt;
GridView1.DataBind();
}

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        int i;
        string sql = "update ............";
        OleDbCommand rateSqlCmd = new OleDbCommand(sql, DBHelper.conn);
        try
        {
             i = rateSqlCmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            throw;
        }
        if(i>0)
        {
            //成功
        }
    }
}

}

试下

kawun
kawun 我是想通过直接修改 datagridview 里面的数据后 点击更改按钮 直接进行增删改查
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!