2014-03-12 28 views
0

我想繼續添加數據在當前行的網格,除非我按下清除按鈕來改變行,但是當我開始添加數據在新增加的行它創建一個新的行。我想保留以前的行。任何幫助!繼續添加數據在當前行,除非我按清除按鈕來改變行

public partial class WebForm5 : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!(IsPostBack)) 
     { 
      Session["List"] = null; 
      ViewState["i"] = ViewState["j"] = 1; 
      ViewState["state"] = ""; 


     } 
    } 
    public static DataTable dt; 
    public static int i = 1; 
    protected void txtName_Click(object sender, EventArgs e) 
    { 

     Session["List"] = dt; 
     dt = new DataTable(); 
     DataRow dr = null; 

     dt.Columns.Add(new DataColumn("RowNumber", typeof(int))); 
     dt.Columns.Add(new DataColumn("Column1", typeof(string))); 
     if (Convert.ToInt16 (ViewState ["i"]) ==Convert.ToInt16(ViewState["j"])) 
     { 

      // here i have to add something 
       dr = dt.NewRow(); 
       ViewState["state"] = txtName.Text; 
       dr["RowNumber"] = i; 
       dr["Column1"] = Convert.ToString(ViewState["state"]) + txtName.Text; 
       dt.Rows.Add(dr); 


     } 
     else if (Convert .ToInt16 (ViewState["i"])!= Convert .ToInt16 (ViewState["j"])) 
     { 

      if (Session["List"] != null) 
      { 
       dt = (DataTable)Session["List"]; 
       dr = dt.NewRow(); 
       dr["Column1"] = txtName.Text; 
       dr["RowNumber"] = i; 
       ViewState["state"] = txtName.Text; 
       dt.Rows.Add(dr); 
       //lst = (List<string>)Session["List"]; 
       //lst.Insert(i,txtAdd.Text); 
       //i++; 

      } 

      ViewState["i"] = ViewState["j"]; 
     } 
     grdData.DataSource = dt; 
     grdData.DataBind(); 
    } 

    protected void btnClr_Click(object sender, EventArgs e) 
    { 
     ViewState["j"] = i++; 
    } 
} 

回答

0

公共部分類WebForm5:System.Web.UI.Page { 保護無效的Page_Load(對象發件人,EventArgs的){ 如果 (!(的IsPostBack)){ 屆 [ 「清單」 ] = null; ViewState [「i」] = ViewState [「j」] = 1; ViewState [「state」] =「」;

 } 
    } 
    public static DataTable dt; 
    public static int i = 1; 
    protected void txtName_Click(object sender, EventArgs e) 
    { 

     Session["List"] = dt; 
     dt = new DataTable(); 
     DataRow dr = null; 

     dt.Columns.Add(new DataColumn("RowNumber", typeof(int))); 
     dt.Columns.Add(new DataColumn("Column1", typeof(string))); 
     if (Session["List"] == null) 
     { 
      if (Convert.ToInt16(ViewState["i"]) == Convert.ToInt16(ViewState["j"])) 
      { 
       dr = dt.NewRow(); 
       dr["RowNumber"] = i; 
       dr["Column1"] = Convert.ToString(ViewState["state"]) + txtName.Text; 
       ViewState["state"] = Convert.ToString(ViewState["state"]) + txtName.Text; 
       dt.Rows.Add(dr); 



      } 
      grdData.DataSource = dt; 
      grdData.DataBind(); 
     } 

       if (Session["List"] != null) 
      { 
     if (Convert .ToInt16 (ViewState["i"])!= Convert .ToInt16 (ViewState["j"])) 
     { 


       dt = (DataTable)Session["List"]; 
       dr = dt.NewRow(); 
       dr["Column1"] = txtName.Text; 
       dr["RowNumber"] = i; 

       dt.Rows.Add(dr); 
       //lst = (List<string>)Session["List"]; 
       //lst.Insert(i,txtAdd.Text); 
       //i++; 
       grdData.DataSource = dt; 
       grdData.DataBind(); 
      } 
     else if (Convert.ToInt16(ViewState["i"]) == Convert.ToInt16(ViewState["j"])) 
     { 
      dt = (DataTable)Session["List"]; 
     ViewState["state"] = grdData.Rows[((grdData.Rows.Count) - 1)].Cells[1].Text; 
     string i =Convert.ToString(ViewState["state"]); 
     int j = dt.Rows.Count; 
      dt.Rows[(dt.Rows.Count)-1]["Column1"] =Convert.ToString(ViewState["state"])+ txtName.Text; 
      grdData.DataSource = dt; 
      grdData.DataBind(); 
     } 

      ViewState["i"] = ViewState["j"]; 
     } 
     txtName.Text = ""; 


    } 

    protected void btnClr_Click(object sender, EventArgs e) 
    { 
     ViewState["j"] = i++; 
     ViewState["state"] = ""; 
     // txtName.Text = grdData.Rows[(grdData.Rows.Count - 1)].Cells[1].Text; 

    } 

    protected void btnnew_Click(object sender, EventArgs e) 
    { 

     dt.Rows[(grdData.Rows.Count - 1)]["Column1"] = txtName.Text; 
     grdData.DataSource = dt; 
     grdData.DataBind(); 
    } 
}