Gridview 不会在单击按钮时添加另一行,它只会添加一行,并且仅更新该行,如何解决此问题



所以我有一个从数据库中填充的下拉列表,目标是从下拉列表中获取所选值并放入网格视图中。问题是它只添加一行,并且仅在我尝试从下拉列表中选择新值时才更新该行。

我试过做数据行 = dt.newrow(( 但没有运气

按钮单击的后端代码

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

        }
    }

第一次从下拉列表中选择值时的第一张图像

第二个图像是当我从下拉列表中选择新值时,但它更新了以前的表并且没有在网格视图中添加新行

在添加按钮上单击,您正在从数据库中检索新数据并创建新的数据表,这就是丢失先前选择的数据的原因。

您需要在两次单击"添加"按钮之间维护先前检索到的数据。

您需要在 aspx.cs 中创建单独的 DataTable 并将其存储在 ViewState 中,并在单击按钮时向该表添加新行,然后将其重新绑定到 GridView。

您需要编写以下代码来实现此目的。

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

现在,按钮的单击事件处理程序应调用上述方法,如下所示。

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

这应该可以帮助您解决问题。

最新更新