我正在尝试创建一个网格视图表,我想在其中显示复选框列表中的一些选定数据。问题是,每当我从复选框列表中选择一些数据时,即使我选中了多个框来显示数据,Gridview仍只会显示顶部复选框中的数据,而不会与列表中的每个框绑定多个不同的数据?

Det gridview确实在选中新框时添加了新行,但是新行复制了前一行的数据

gridview和复选框列表都连接到SQL数据库,数据来自于该数据库:



 public partial class Visual : System.Web.UI.Page
 {
    String con = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        DataRow dr = dt.NewRow();
        dt.Columns.Add("SensorID");
        dt.Columns.Add("BatteryLife");
        dt.Columns.Add("YearInUsage");
        dt.Columns.Add("NumberOfUsage");
        dt.Columns.Add("Occupations");
        dt.Columns.Add("Placement");
        dt.Columns.Add("Zip");
        dt.Columns.Add("City");

        List<DataListe> dl = new List<DataListe>();

        foreach (ListItem item in CheckBoxList1.Items)
        {
            SqlConnection sc = new SqlConnection(con);
            sc.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM Statistic WHERE SensorID='" + CheckBoxList1.SelectedValue + "'", sc);
            SqlDataReader reader = cmd.ExecuteReader();

            if (item.Selected)
            {
                while (reader.Read())
                {
                    DataListe dali = new DataListe();

                    string si = (string)reader["SensorID"];
                    dali.SensorID = si;

                    string bl = (string)reader["BatteryLife"];
                    dali.BatteryLife = bl;

                    string yu = (string)reader["YearInUsage"];
                    dali.YearInUsage = yu;

                    int nu = (int)reader["NumberOfUsage"];
                    dali.NumberOfUsage = nu;

                    string oc = (string)reader["Occupations"];
                    dali.Occupations = oc;

                    string pl = (string)reader["Placement"];
                    dali.Placement = pl;

                    int zi = (int)reader["Zip"];
                    dali.Zip = zi;

                    string ci = (string)reader["City"];
                    dali.City = ci;


                    dl.Add(dali);

                }
            }
            sc.Close();
        }

        GridView1.DataSourceID = null;
        GridView1.DataSource = dl;
        GridView1.DataBind();

        return;
}


我希望当选中复选框列表中的新框时,gridview会添加新行,并保持前一行不变。该行应包含与复选框列表中的项目相关的信息,该信息由SQLdatabase提供。

但是,当选中一个新框时,gridview确实会添加一个新行,但是它会复制已经显示的行中的数据。

最佳答案

您的代码中的问题是您使用CheckboxList1.SelectedValue。 CheckBoxList的SelectedValue应该是什么?您是否认为无论此值是多少,当您遍历Items集合时它都会改变吗?

您需要改用Item.Value

// Opening the connection just one time before starting the loop
using(SqlConnection sc = new SqlConnection(con))
{
    sc.Open();
    string cmdText = "SELECT * FROM Statistic WHERE SensorID=@id";
    foreach (ListItem item in CheckBoxList1.Items)
    {
        if (item.Selected)
        {
            // Moved the actual query inside the check for the item.Selected
            // so you are calling the db only for the items that you want to use
           SqlCommand cmd = new SqlCommand(cmdText, sc);
           cmd.Parameters.Add("@id", SqlDbType.NVarChar).Value = item.Value;
           using(SqlDataReader reader = cmd.ExecuteReader())
           {
               while (reader.Read())
               {
                   DataListe dali = new DataListe();
                   dali.SensorID = (string)reader["SensorID"];
                   dali.BatteryLife = (string)reader["BatteryLife"];
                   dali.YearInUsage = (string)reader["YearInUsage"];
                   dali.NumberOfUsage = (int)reader["NumberOfUsage"];
                   dali.Occupations = (string)reader["Occupations"];
                   dali.Placement = (string)reader["Placement"];
                   dali.Zip = (int)reader["Zip"];
                   dali.City = (string)reader["City"];
                   dl.Add(dali);
             }
        } // At this point the reader is closed and disposed by the ending using block
    }
}  // At this point the connection is closed and disposed by the ending using block


其他要注意的事项:当不再需要连接和读取器之类的一次性对象时,应将其丢弃。 using语句即使在出现异常的情况下也可以确保这一点。另一个非常重要的方面是当您要将查询文本传递到数据库引擎时使用参数。将字符串串联在一起是一种确定自己是否遇到Sql Injection和可能的解析错误的可靠方法。

关于c# - 从复选框列表顶部的项目中从SQL数据库中选择的数据被复制到所有Gridview行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56703362/

10-11 01:04
查看更多