我想实现的功能是,数据库JCXT中事先有表Table_gjfirst,其中字段gjfirst_control是检查内容,其他字段值都为空,但用户在浏览器中需要根据gjfirst_control字段内容填写其他字段,就像在填表一样,最后将结果统一传回数据库。
datagrid表已经邦定,各个字段我都“编辑模板”,在itemtemplate中放入TEXT文本框,设它边框为无,则效果就和真正填表一样,只能看到单元格中的焦点了。
但是,所有单元格都填好后,我要怎样编写“提交”代码,才能将所有数据传给数据库呢?我在这里虚心讨教各位了,多谢!
下面是我的代码(缺提交代码):private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
SetBind();
}
} private void SetBind()
{
SqlConnection con=new SqlConnection("server=〈我的ip地址〉;database=JCXT;uid=sa;pwd=123;");
SqlDataAdapter da=new SqlDataAdapter("select * from Table_gjfirst",con);
DataSet ds=new DataSet();
da.Fill(ds,"table1");
this.DataGrid1.DataSource=ds.Tables["table1"];
this.DataGrid1.DataBind();
}
private void Button1_Click(object sender, System.EventArgs e)
{
//在此编写代码点击“提交”按钮后,每个单元格中的数据都传回数据库。不知如何写?
}

解决方案 »

  1.   

    取数据:
    SqlDataAdapter->DataSet->DataGrid
    存数据:
    DataGrid->DataSet->SqlDataAdapter(Update)
      

  2.   


    public DataSet CreateCmdsAndUpdate(DataSet myDataSet,string myConnection,string mySelectQuery,string myTableName) 
    {
        OleDbConnection myConn = new OleDbConnection(myConnection);
        OleDbDataAdapter myDataAdapter = new OleDbDataAdapter();
        myDataAdapter.SelectCommand = new OleDbCommand(mySelectQuery, myConn);
        OleDbCommandBuilder custCB = new OleDbCommandBuilder(myDataAdapter);    myConn.Open();    DataSet custDS = new DataSet();
        myDataAdapter.Fill(custDS);    //code to modify data in dataset here    myDataAdapter.Update(custDS, myTableName);    myConn.Close();    return custDS;
     }
      

  3.   

    供参考:
    1、使用SqlCommandBuilder(必须有主键)
    myDataAdapter.SelectCommand = new SqlCommand(mySelectQuery, myConn);
    SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter);
    //修改dataset里的数据
    myDataAdapter.Update(ds, myTableName);2、写UpdateCommand
    cmd = new SqlCommand("UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
                           "WHERE CustomerID = @oldCustomerID", conn);  cmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
      cmd.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");  parm = cmd.Parameters.Add("@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
      parm.SourceVersion = DataRowVersion.Original;  da.UpdateCommand = cmd;这两个方法应该都可以,不好意思,没有按你的代码改。不难,试试,你可以的
      

  4.   

    这是我现在的代码,没有错误提示,但仍不能实现更新。究竟那里不对?帮我看看
    private void Page_Load(object sender, System.EventArgs e)
    {
            ds = new DataSet(); 
            if(!IsPostBack)
    {
    SetBind();
    }
    }
    private void SetBind()
    {
             SqlConnection con=new SqlConnection("server=192.168.1.222;database=JCXT;uid=sa;pwd=123;");
    SqlDataAdapter da=new SqlDataAdapter("select * from Table_gjfirst",con);
    da.SelectCommand = new SqlCommand("select * from Table_gjfirst", con);
    SqlCommandBuilder cb = new SqlCommandBuilder(da);
    DataSet ds=new DataSet();
    da.Fill(ds,"table1");
    this.DataGrid1.DataSource=ds.Tables["table1"];
    this.DataGrid1.DataBind();
    }
    private void btnUpdate_Click(object sender, System.EventArgs e)
    {
      if (ds.HasChanges()) 
         { 
    foreach (DataGridItem di in DataGrid1.Items)
    { DataRow dr=ds.Tables["table1"].Rows.Find(DataGrid1.DataKeys[di.ItemIndex]);
    dr["gjfirst_problem"]=((TextBox)di.FindControl("gjfirst_problem")).Text;
    dr["gjfirst_findtime"]=((TextBox)di.FindControl("gjfirst_findtime")).Text;
    dr["gjfirst_checkpeople"]=((TextBox)di.FindControl("gjfirst_people")).Text;
    }
         } 
              setBind();
    }
    }
    }
      

  5.   

    好多天没解决了,好像只差一点了。
    错误提示:未将对象引用设置到对象的实例。private void SetBind()
    {SqlConnection con=new SqlConnection("server=192.168.1.222;database=JCXT;uid=sa;pwd=123;");
    SqlDataAdapter da=new SqlDataAdapter();
    da.SelectCommand = new SqlCommand("select * from Table_gjfirst", con);
    SqlCommandBuilder cb=new SqlCommandBuilder(da);
    DataSet ds=new DataSet();
    da.Fill(ds,"table1");
    this.DataGrid1.DataSource=ds.Tables["table1"];
    this.DataGrid1.DataBind();
    }private void btnUpdate_Click(object sender, System.EventArgs e)
    {
    foreach (DataGridItem di in DataGrid1.Items)
     {
           SqlCommand = new SqlCommand("update Table_gjfirst set gjfirst_problem='"+((TextBox)di.FindControl("txt_problem")).Text+"',gjfirst_findtime='"+((TextBox)di.FindControl("txt_findtime")).Text+"',gjfirst_checkpeople='"+((TextBox)di.FindControl("txt_checkpeople")).Text+"' where gjfirst_control='"+((Label)di.FindControl("lab_control")).Text+"'");
           da.UpdateCommand = SqlCommand;
           SetBind();
     }
    }
      

  6.   

    zy_seven(我想知道)  哥哥的第一方法很好用,但你的记录表必须要有个主键,
    不然是不会更新的~~你上边的方法,是不是有的对像没有找到啊
      

  7.   

    应该是没找到,不过我用的就是第一方法,可是不知道错在那里,错误提示行在:
    SqlCommand = new SqlCommand("update Table_gjfirst set gjfirst_problem='"+((TextBox)di.FindControl("txt_problem")).Text+"',gjfirst_findtime='"+((TextBox)di.FindControl("txt_findtime")).Text+"',gjfirst_checkpeople='"+((TextBox)di.FindControl("txt_checkpeople")).Text+"' where gjfirst_control='"+((Label)di.FindControl("lab_control")).Text+"'");
    请给意见,多谢!
      

  8.   

    我的主键是EmployeeID,在程序的什么地方应用它.请问?
      

  9.   

    完全用第一种方法,我的代码改成下面:
    private void SetBind()
    {SqlConnection con=new SqlConnection("server=192.168.1.222;database=JCXT;uid=sa;pwd=123;");
    SqlDataAdapter da=new SqlDataAdapter();
    da.SelectCommand = new SqlCommand("select * from Table_gjfirst", con);
    SqlCommandBuilder cb=new SqlCommandBuilder(da);
    DataSet ds=new DataSet();
    da.Fill(ds,"table1");
    this.DataGrid1.DataSource=ds.Tables["table1"];
    this.DataGrid1.DataBind();
    }private void btnUpdate_Click(object sender, System.EventArgs e)
    {
     da.Update(ds,"table1");
    }错误提示为:Update 无法找到 TableMapping['table1'] 或 DataTable“table1”。
      

  10.   

    我也遇到过这个问题, 没有解决, 后来还是老实的用UPDATE 表 SET 字段=赋值 Where id=值 这个操作一条条更新完成的。 把整个DATASET更新到数据库实际上操作太难了!
        等待达人给出容易解决的答案!
      

  11.   

    用了一个星期终于成功实现了编辑DataGrid批量更新,代码很简单,感谢大家关注。散分!