Viewstate添加,删除行

时间:2017-04-13 10:43:54

标签: c# asp.net gridview viewstate

我有两个问题,添加,从GridView删除一行第一个: - 是当我添加一些行时,它会在第一行自动添加空行。我使用的代码

    protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        initial();
        //bill_date.Text = DateTime.Now.ToShortDateString();
        add_bill_GridView.DataSource = null;
        add_bill_GridView.DataBind();
        using (SupermarketEntities1 db = new SupermarketEntities1())
        {
            ddl_choose_item.DataSource = db.Items.ToList();
            ddl_choose_item.DataTextField = "item_name";
            ddl_choose_item.DataValueField = "item_id";
            ddl_choose_item.DataBind();
        }
    }

}

    //this put on page_load
private void initial()
{
    //creating DataTable  
    DataTable dt = new DataTable();
    DataRow dr;
    dt.TableName = "ProductsSold";

    //creating columns for DataTable  
    dt.Columns.Add("ddl_choose_item", typeof(string));
    dt.Columns.Add("txt_price", typeof(string));
    dt.Columns.Add("txt_discount", typeof(string));
    dt.Columns.Add("txt_quantitiy", typeof(string));
    dt.Columns.Add("txt_total", typeof(string));

    dr = dt.NewRow();
    dt.Rows.Add(dr);

    ViewState["ProductsSold"] = dt;
    add_bill_GridView.DataSource = dt;
    add_bill_GridView.DataBind();
}

//this put in btn_add_click 
private void add_new_row()
{
    if (ViewState["ProductsSold"] != null)
    {
        DataTable dtCurrentTable = (DataTable)ViewState["ProductsSold"];
        DataRow drCurrentRow = null;

        if (dtCurrentTable.Rows.Count > 0)
        {
            for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
            {
                //Creating new row and assigning values  
                drCurrentRow = dtCurrentTable.NewRow();
                drCurrentRow["ddl_choose_item"] = ddl_choose_item.SelectedItem;
                drCurrentRow["txt_price"] = txt_price.Text;
                drCurrentRow["txt_discount"] = txt_discount.Text;
                drCurrentRow["txt_quantitiy"] = txt_quantitiy.Text;
                drCurrentRow["txt_total"] = txt_total.Text;
            }

            //Added New Record to the DataTable  
            dtCurrentTable.Rows.Add(drCurrentRow);
            //storing DataTable to ViewState  
            ViewState["ProductsSold"] = dtCurrentTable;
            //binding Gridview with New Row  
            add_bill_GridView.DataSource = dtCurrentTable;
            add_bill_GridView.DataBind();
        }
    }
}

//btn_add_Click
protected void btn_add_Click(object sender, EventArgs e)
{
    add_new_row();
}

第二个问题是,当我添加一些行并想要删除其中一些并再次添加新行时,它正常工作,但如果我删除了所有行,我就不能再添加了。我使用的代码

    protected void BindGrid()
{
    add_bill_GridView.DataSource = ViewState["ProductsSold"] as DataTable;
    add_bill_GridView.DataBind();
}

protected void add_bill_GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    int index = Convert.ToInt32(e.RowIndex);
    DataTable dt = ViewState["ProductsSold"] as DataTable;
    dt.Rows[index].Delete();
    ViewState["ProductsSold"] = dt;
    BindGrid();
}

最后一个问题: - 用jquery或者我喜欢做的更好吗?他们之间的区别是什么,而不是处理服务器而不是? 感谢。

2 个答案:

答案 0 :(得分:1)

好好看看。

初​​始():

Error: 404 - no such index

添加新行方法:

private void initial()
{
    //creating DataTable  
    DataTable dt = new DataTable();
    DataRow dr;
    dt.TableName = "ProductsSold";

    //creating columns for DataTable  
    dt.Columns.Add("ddl_choose_item", typeof(string));
    dt.Columns.Add("txt_price", typeof(string));
    dt.Columns.Add("txt_discount", typeof(string));
    dt.Columns.Add("txt_quantitiy", typeof(string));
    dt.Columns.Add("txt_total", typeof(string));

    //dr = dt.NewRow();
    //dt.Rows.Add(dr);

    ViewState["ProductsSold"] = dt;
    add_bill_GridView.DataSource = dt;
    add_bill_GridView.DataBind();
}

在上面的代码中,我评论了一些内容,请检查并告诉我。

答案 1 :(得分:0)

我发现了这个问题......

你的观点永远不会变为空。所以当你删除所有行viewstate not == null并且两个条件都变为false。

解决方案:

设置一个条件

 if (GridView1.Rows.Count == 0)
            {
                ViewState["ProductsSold"] = null;
            }

让我知道是否仍然混淆......