保持在当前行中添加数据,除非我按清除按钮更改行

本文关键字:非我 清除 按钮 数据 添加 | 更新日期: 2023-09-27 17:49:55

我想继续添加数据在当前行的网格,除非我按下清除按钮改变行,但当我开始添加数据在新的添加行它创建一个新的行。我想保留之前的行。任何帮助!!

   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++;
    }
}

保持在当前行中添加数据,除非我按清除按钮更改行

公共部分类WebForm5: System.Web.UI.Page{Page_Load(对象发送者,EventArgs e){如果(! (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 (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();
    }
}