0

大家好,我正在为某些操作(例如在每个按钮上)分配视图状态的数据表,我将分配一行并将其绑定到数据表并将该数据表分配给视图状态。我有一个与我正在创建的数据表绑定的网格视图。因为我在删除行时有一个删除选项,所以我清除了 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();
            }
        }

    }

但是,在删除后查看它时,数据表仍然与具有先前值的数据表相同,有人可以告诉我哪里出错了

4

2 回答 2

1

grid您必须在从中删除行后再次绑定datatable。还要确保如果在 page_load 中绑定 gridview,那么它在 if(!Page.IsPostBack) 中。

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;
   //You have to bind the grid with new data table.
   GridView1.DataSource =  dt;
   GridView1.DataBind();
}
于 2013-04-01T08:48:58.143 回答
0

在页面加载事件中检查您的 !isPostBack 内的 gridview 绑定方法调用?

见下文

void page_load()
{
if(!IsPostBack)    
{
// call Bind grid method
}
}
于 2013-04-01T08:54:59.483 回答