Search code examples
c#asp.netgridviewviewstate

Viewstate add,delete row


I have 2 problems with adding, deleting a row from GridView The first: - is that when i adding some rows it adds blank row automatically on the first. And that the code i use

    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();
}

The second problem is when i add some rows and want to delete some of it and add new rows again, it works normally, but if i deleted all rows i can't add again anymore. And that the code i use

    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();
}

last question: - is that better to do it with jquery or like what i did ? What is the difference between them more than that dealing with the server and that not ? Thanks.


Solution

  • kindly have a look.

    initial():

    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();
    }
    

    add new row method:

    private void add_new_row()
        {
            if (ViewState["ProductsSold"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["ProductsSold"];
                DataRow drCurrentRow;
    
                //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";
                        drCurrentRow["txt_price"] = "pric";
                        drCurrentRow["txt_discount"] = "dis";
                        drCurrentRow["txt_quantitiy"] = "quan";
                        drCurrentRow["txt_total"] = "tot";
                    //}
    
                    //Added New Record to the DataTable  
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    //storing DataTable to ViewState  
                    ViewState["ProductsSold"] = dtCurrentTable;
                    add_bill_GridView.DataSource = dtCurrentTable; 
                    add_bill_GridView.DataBind();
                //}
            }
        }
    

    In the above code, i have commented some of the line, kindly check and let me know.