大家好,我正在为某些操作(例如在每个按钮上)分配视图状态的数据表,我将分配一行并将其绑定到数据表并将该数据表分配给视图状态。我有一个与我正在创建的数据表绑定的网格视图。因为我在删除行时有一个删除选项,所以我清除了 Viewstate 并为其分配了新的数据表
DataTable dtPayment = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void btn_Click(object sender, EventArgs e)
{
if (ViewState["CurrentTable"] == null)
{
dtPayment.Columns.Add("Id", typeof(string));
dtPayment.Columns.Add("PaymentMode", typeof(string));
dtPayment.Columns.Add("TransactionAmount", typeof(string));
dtPayment.Columns.Add("BankCharges", typeof(string));
dtPayment.Columns.Add("Particulars", typeof(string));
DataRow dr = dtPayment.NewRow();
dr["Id"] = 1;
dr["PaymentMode"] = "Cheque";
dr["TransactionAmount"] = txtAmount.Text;
dr["BankCharges"] = "";
dr["Particulars"] = "";
dtPayment.Rows.Add(dr);
ViewState["CurrentTable"] = dtPayment;
grd.DataSource = dtPayment;
grd.DataBind();
}
else
{
AddNewRowToGrid("Cheque", txtAmount.Text);
}
}
protected void grd_Deleting(object sender, DeletingEventArgs e)
{
dt = (DataTable)ViewState["CurrentTable"];
int iIndex;
iIndex = grd.FocusedRowIndex;
dt.Rows[iIndex].Delete();
dt.AcceptChanges();
ViewState.Remove("CurrentTable");
ViewState["CurrentTable"] = dt;
grd.DataSource = dt;
grd.DataBind();
}
protected void AddNewRowToGrid(string strPaymntMode, string transAmount)
{
int rowIndex = 0;
//SetPreviousData();
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["Id"] = i + 1;
drCurrentRow["PaymentMode"] = strPaymntMode;
drCurrentRow["TransactionAmount"] = transAmount;
drCurrentRow["BankCharges"] = "";
drCurrentRow["Particulars"] = "";
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
grd.DataSource = dtCurrentTable;
grd.DataBind();
}
}
}
但是,在删除后查看它时,数据表仍然与具有先前值的数据表相同,有人可以告诉我哪里出错了