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