用一个DATATABLE添加一行,然后绑定到DATAGRID,在DATAGRID里编辑后,怎么把编辑后的数据再重新绑定到DATATABLE.

解决方案 »

  1.   

    把编辑后的数据放到数据库里,然后在绑定到DATATABLE。
      

  2.   

    //绑定数据表到数据网格
    datagrid1.datasource = datatable1;
    datagrid1.datamember = "tablename";//在DataGrid中编辑数据....//更新数据库
    SqlDataAdapter.Update(this.datatable1.getchanges());
    this.datatable1.AcceptChanges();
      

  3.   

    我就是想在不更新数据库的情况下更新datatable啊
      

  4.   

    那有什么作用?这样的话直接调用:this.datatable1.AcceptChanges(); 即可
      

  5.   

    我的意思是让datatable添加第二行的时候,第一行的数据不刷新.我现在一添加第二行,第一行的数据就恢复成原来的默认值了
      

  6.   

    this.datatable1.AcceptChanges(); 然后重新绑定一下
      

  7.   

    private void additem_Click(object sender, System.EventArgs e)
    {
    DataRow row = table.NewRow();
    row[0]=(char)(table.Rows.Count+65);
    row[1] ="";
    row[2]=(char)(table.Rows.Count+65);
    table.Rows.Add(row);
    this.exam.DataBind();
    }
    还是没用啊,这个是代码,ROW[1]对应的是DATAGRID里面的一个模板列,里面是一个TXTXBOX,我的意思是TXETBOX已经编辑过了,第二次按这个按钮的时候TEXTBOX里面编辑的内容不刷新
      

  8.   

    关键是在改好之后要记得datatable1.AcceptChanges();
      

  9.   

    private void additem_Click(object sender, System.EventArgs e)
    {
    DataRow row = table.NewRow();
    row[0]=(char)(table.Rows.Count+65);
    row[1] ="";
    row[2]=(char)(table.Rows.Count+65);
    table.Rows.Add(row);
             this.exam.DataSource= table;
    this.exam.DataBind();
    }
      

  10.   

    table.Rows.Add(row);以后要调用ACCEPTCHANGES()函数这样这个行才能真正的加入到DATATABLE里去
      

  11.   

    用游离编辑啦,自己放个textbox上去,定位的话就用:
    this.textBox1.Left=this.dataGrid1.GetCellBounds(this.dataGrid1.CurrentCell).X+this.dataGrid1.Left ;
    this.textBox1.Top=this.dataGrid1.GetCellBounds(this.dataGrid1.CurrentCell).Y+this.dataGrid1.Top;
    this.textBox1.Width=this.dataGrid1.GetCellBounds(this.dataGrid1.CurrentCell).Width;
    this.textBox1.Height=this.dataGrid1.GetCellBounds(this.dataGrid1.CurrentCell).Height;
    然后this.textBox1.Text=ds.Tables[0].Rows[this.dataGrid1.CurrentCell.RowNumber][this.dataGrid1.CurrentCell.ColumnNumber].ToString(); 可以获取选择的网格的值,接下来当你鼠标选取下一个单元格时,把文本框的内容更新到上一次选择的单元格即可,然后文本框再获取现在的单元格的值。也就是说,还需要定义一个保存上一次选择的单元格的一个变量。
      

  12.   

    http://blog.csdn.net/zhzuo/archive/2005/01/03/238273.aspx
    http://blog.csdn.net/zhzuo/archive/2004/08/06/67037.aspx
      

  13.   

    this.datatable1.getchanges();
    SqlDataAdapter.Update();
    this.datatable1.AcceptChanges();
    getchanges是设置行的状态,包括:updated,deleted,inserted
    Update是根据上面的几个状态判断应该用SqlDataAdapter的语句,提交到数据库。AcceptChanges()只是把updated,deleted,inserted状态恢复!如果先调用AcceptChanges(),在update是不会更新等等。
      

  14.   

    /// <param name="e"></param>
    private void btn_Save_Click(object sender, System.EventArgs e)
    {
    ComLogOutput.WriteLogFile("Button Save Click Begin"); if(!CheckUpdateMustInput())
    {
    return;
    }
    if(!CheckUpdateStyleInput())
    {
    return;
    } DataSet dsUpt = GetUpdateDataSet(); if(dsUpt.GetChanges() == null)
    {
    //lbl_Err.Text = "No data changed,please input again";
    lbl_Err.Text = ComLocalDiff.GetMessageById("0016",UserInfo.LangCode); }
    else
    {
    UpdateDataBase(dsUpt);
    } ComLogOutput.WriteLogFile("Button Save Click END");
    }/// <summary>
    /// change in ds
    /// </summary>
    /// <returns></returns>
    private DataSet GetUpdateDataSet()
    {
    //Creat a new datatable
    DataSet dsUpt = null;

    if(this.Session["dsp_MstArea_DgData"]==null)
    {
    if(!SearchData())
    {
    return dsUpt;
    }
    } //Copy the construct of the dataset
    DataSet dsOri = (DataSet)this.Session["dsp_MstArea_DgData"];
    dsUpt= dsOri.Copy(); // dsUpt index
    int intDs = 0; for(int i = 0; i < dg_MstArea.Items.Count;i++)
    {
    // get dsUpt index
    intDs = GetDsCorrespondId(dg_MstArea,i); string str1 = ((UC_AreaCode)this.dg_MstArea.Items[i].FindControl("dpl_dgAreaCode")).GetSelectValue;
    string str2 = ((TextBox)this.dg_MstArea.Items[i].FindControl("txt_dgCountryName")).Text;
    string str3 = (((UC_CalendarText)this.dg_MstArea.Items[i].FindControl("uc_dgEffDate")).Text);
    string str4 = (((TextBox)this.dg_MstArea.Items[i].FindControl("txt_dgCountryNameSc")).Text);
    string str5 = (((TextBox)this.dg_MstArea.Items[i].FindControl("txt_dgCountryNameTc")).Text); // whether change
    if(str1.Equals((dsUpt.Tables[0].Rows[intDs]["AREA_CODE"].ToString())) &&
    str2.Equals((dsUpt.Tables[0].Rows[intDs]["COUNTRY_NAME"].ToString())) &&
    str4.Equals((dsUpt.Tables[0].Rows[intDs]["COUNTRY_NAME_SC"].ToString())) &&
    str5.Equals((dsUpt.Tables[0].Rows[intDs]["COUNTRY_NAME_TC"].ToString())) &&
    str3.Equals((dsUpt.Tables[0].Rows[intDs]["EFF_DATE_SHOW"].ToString())))
    {
    continue;
    }
    else
    {
    dsUpt.Tables[0].Rows[intDs]["AREA_CODE"] = str1;
    dsUpt.Tables[0].Rows[intDs]["COUNTRY_NAME"] = str2;
    dsUpt.Tables[0].Rows[intDs]["COUNTRY_NAME_SC"] = str4;
    dsUpt.Tables[0].Rows[intDs]["COUNTRY_NAME_TC"] = str5;
    dsUpt.Tables[0].Rows[intDs]["EFF_DATE"] = str3;
    dsUpt.Tables[0].Rows[intDs]["UPDT_UID"] = UserInfo.UID;
    dsUpt.Tables[0].Rows[intDs]["UPDT_DATE"] = DateTime.Now;
    }
    } return dsUpt;
    }/// research data
    /// </summary>
    private bool SearchData()
    {
    DataSet dsBind = null; try
    {
    string strSql = GetSearchSql(); MstAreaDB mstDB =  new MstAreaDB();
    dsBind = mstDB.GetDGDate(strSql);
    Session["dsp_MstArea_DgData"]=dsBind; if(dsBind.Tables[0].Rows.Count == 0)
    {
    //this.lbl_Err.Text= "there is no data";
    lbl_Err.Text = ComLocalDiff.GetMessageById("0015",UserInfo.LangCode);
    SetResultPage(false);
    return false;
    }

    return true;
    }
    catch(Exception ex)
    {
    ComLogOutput.WriteLogFile(ex);
    lbl_Err.Text = ComLocalDiff.GetMessageById("0011",UserInfo.LangCode);
    return false;
    }
    } /// <summary>
    /// update delete in db
    /// </summary>
    /// <param name="dstUpt"></param>
    private void UpdateDataBase(DataSet dsUpt)
    {
    try
    {
    string strSql = GetSearchSql(); //update data
    MstAreaDB mstDB = new MstAreaDB();
    mstDB.UpdateArea(dsUpt,strSql); //research data
    this.Session["dsp_MstArea_DgData"]=null;
    BindDataGrid(dg_MstArea.CurrentPageIndex); //ComJScript.AlertNormal(this,"Action complete sucessfully!");
    ComJScript.AlertNormal(this,ComLocalDiff.GetMessageById("0018",UserInfo.LangCode));
    }
    catch(System.Data.OleDb.OleDbException sqlEx)
    {
    ComLogOutput.WriteLogFile(sqlEx);
    if(sqlEx.ErrorCode == -2147217873)
    {
    //this.lbl_Err.Text="key duplicate";
    lbl_Err.Text = ComLocalDiff.GetMessageById("0008",UserInfo.LangCode);
    }
    }
    catch(DBConcurrencyException ex)
    {
    ComLogOutput.WriteLogFile(ex);
    //db has been updated by other user
    //this.lbl_Err.Text= "DB has been updated by other user, please research data.";
    lbl_Err.Text = ComLocalDiff.GetMessageById("0009",UserInfo.LangCode);
    }
    catch(Exception ex)
    {
    ComLogOutput.WriteLogFile(ex);
    lbl_Err.Text = ComLocalDiff.GetMessageById("0001",UserInfo.LangCode);
    }
    }