Asisk
Asisk

Reputation: 15

Gridview wont add another row on button click, it only adds one row and it updates that row only, how to fix this?

So I have a dropdown list that is being populate from database, the goal is to get the selected values from the dropdown list and put in the gridview. The problem is it only adds one row and only updates that row when I try to select new values from the dropdownlist.

I have tried doing Datarow row = dt.newrow() but not luck

Back End code of button click

 string CS = ConfigurationManager.ConnectionStrings["POS_SystemConnectionString2"].ConnectionString;

        using (SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM Product_Details WHERE Model = '" + ddlModel.SelectedItem.Text + "' AND Price = '" +ddlPrice.SelectedItem.Text + "'", con);
            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();


        }
    }

First image when I select values from dropdown list for first time

Second image is when I select new values from dropdown list but it updates previous table and does not add new row in gridview

Upvotes: 0

Views: 92

Answers (1)

Chetan
Chetan

Reputation: 6891

On Add Button click you are retrieving new data from database and creating new datatable that's why you are loosing the previously selected data.

You need to maintain the previously retrieved data between two clicks on Add button.

You need to create a separate DataTable in aspx.cs and store it in the ViewState and add new row to that table on button click and re-bind it to the GridView.

You need to write following code to achieve this.

//This method will check if the table exist in the ViewState
//If table does not exist in ViewState, a new table will be created and stored in ViewState
private DataTable GetDataTable()
{
    DataTable dt = ViewState["SelectedModels"] as DataTable;

    if (dt == null)
    {
        dt = new DataTable();
        dt.TableName = "ColorData";
        dt.Columns.Add(new DataColumn("Model", typeof(string)));
        dt.Columns.Add(new DataColumn("Price", typeof(string)));
        ViewState["SelectedModels"] = dt;
    }
    return dt;
}

//This method will store DataTable to ViewState
private void SaveDataTable(DataTable dataTable)
{
    ViewState["SelectedModels"] = dataTable;
}

//This method will get the data from the database, add it to the DataTable
//And it will re-bind the DataTable to the GridView and store the updated DataTable to ViewState
private void AddItemToList(string modelName, string price)
{
    string CS = ConfigurationManager.ConnectionStrings["POS_SystemConnectionString2"].ConnectionString;

    using (SqlConnection con = new SqlConnection(CS))
    {
        using (SqlCommand cmd =
            new SqlCommand("SELECT * FROM Product_Details WHERE Model = @modelName AND Price = @price", con))
        {
            var modelParameter = new SqlParameter();
            modelParameter.ParameterName = "@modelName";
            modelParameter.Value = modelName;
            cmd.Parameters.Add(modelParameter);

            var priceParameter = new SqlParameter();
            priceParameter.ParameterName = "@price";
            priceParameter.Value = price;
            cmd.Parameters.Add(priceParameter);

            con.Open();

            using (var sqlReader = cmd.ExecuteReader())
            {
                var dataTable = GetDataTable();
                while (sqlReader.Read())
                {
                    var dataRow = dataTable.NewRow();
                    //Hear I assume that Product_Details table has Model and Price columns
                    //So that sqlReader["Model"] and sqlReader["Price"] will not have any issue.
                    dataRow["Model"] = sqlReader["Model"];
                    dataRow["Price"] = sqlReader["Price"];
                    dataTable.Rows.Add(dataRow);
                }

                SaveDataTable(dataTable);
                GridView1.DataSource = dataTable;
                GridView1.DataBind();
            }
        }
    }
}

And now the Click Event Handler of the button should call the above method as following.

protected void bttnAdd_Click(object sender, EventArgs e)
{
    AddItemToList(ddlModel.SelectedItem.Text, ddlPrice.SelectedItem.Text);
}

This should help you resolve your issue.

Upvotes: 1

Related Questions