C# 从复选框列表顶部项中从SQL数据库中选择的数据将复制到所有Gridview行
我正在尝试创建一个网格视图表,在该表中,我希望显示复选框列表中的一些选定数据。问题是,每当我从复选框列表中选择一些数据时,gridview将只显示顶部复选框中的数据,而不会显示与列表中每个框关联的多个不同数据,即使我选中了多个复选框以显示其中的数据 选中新框时,Det gridview会添加新行,但新行会复制上一行的数据 gridview和checkboxlist都连接到SQLdatabase,数据来自: 代码C# 从复选框列表顶部项中从SQL数据库中选择的数据将复制到所有Gridview行,c#,sql,asp.net,C#,Sql,Asp.net,我正在尝试创建一个网格视图表,在该表中,我希望显示复选框列表中的一些选定数据。问题是,每当我从复选框列表中选择一些数据时,gridview将只显示顶部复选框中的数据,而不会显示与列表中每个框关联的多个不同数据,即使我选中了多个复选框以显示其中的数据 选中新框时,Det gridview会添加新行,但新行会复制上一行的数据 gridview和checkboxlist都连接到SQLdatabase,数据来自: 代码 public partial class Visual : System.Web.
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;
}
public分部类Visual:System.Web.UI.Page
{
String con=ConfigurationManager.ConnectionString[“ConnectionString”].ConnectionString;
受保护的无效页面加载(对象发送方、事件参数e)
{
}
public void CheckBoxList1\u SelectedIndexChanged(对象发送方,事件参数e)
{
DataTable dt=新的DataTable();
DataRow dr=dt.NewRow();
dt.列。添加(“传感器ID”);
dt.列。添加(“电池寿命”);
dt.列。添加(“年报”);
dt.列。添加(“NumberOfUsage”);
dt.栏。添加(“职业”);
dt.列。添加(“放置”);
dt.列。添加(“Zip”);
dt.列。添加(“城市”);
List dl=新列表();
foreach(CheckBoxList1.Items中的ListItem项目)
{
SqlConnection sc=新的SqlConnection(con);
sc.Open();
SqlCommand cmd=new SqlCommand(“从统计中选择*,其中SensorID='”+CheckBoxList1.SelectedValue+“'”,sc);
SqlDataReader=cmd.ExecuteReader();
如果(选定项)
{
while(reader.Read())
{
DataListe dali=新的DataListe();
字符串si=(字符串)读取器[“SensorID”];
dali.SensorID=si;
字符串bl=(字符串)读取器[“电池寿命”];
dali.BatteryLife=bl;
字符串yu=(字符串)读取器[“YearInUsage”];
dali.YearInUsage=yu;
int nu=(int)读卡器[“NumberOfUsage”];
dali.NumberOfUsage=nu;
字符串oc=(字符串)读卡器[“职业”];
dali.职业=oc;
字符串pl=(字符串)读取器[“位置”];
dali.Placement=pl;
int zi=(int)读卡器[“Zip”];
dali.Zip=zi;
字符串ci=(字符串)读卡器[“城市”];
大理市=ci;
dl.Add(大理);
}
}
sc.Close();
}
GridView1.DataSourceID=null;
GridView1.DataSource=dl;
GridView1.DataBind();
返回;
}
我希望gridview在选中checkboxlist中的一个新框时添加一个新行,并保持前一行的完整性。该行应包含与checkboxlist中的项相关的信息,该项由SQLdatabase提供
但是,当选中一个新框时,gridview确实会添加一个新行,但它会从已经显示的行复制数据。您的问题是您的代码在复选框选中的索引更改事件中。每次您选中另一个条目时,此代码都会触发。由于您在运行数据集之前没有清除该数据集,因此它会在ent中迭代ire复选框列表并添加任何选中的项目。因此,当您选中第一个复选框时,它会将其放入数据集中。如果选中第二个复选框,它会将第一个和第二个复选框放入数据集中,以此类推
将代码移动到“离开事件”或“失去焦点事件”复选框中。它只应在您离开控件时触发。代码中的问题是您使用了复选框列表1。SelectedValue。复选框列表的SelectedValue应该是什么?您是否认为无论该值是什么,它都会在循环过程中发生变化特快专递收款 您需要改用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注入和可能的解析错误方面遇到了麻烦。旁注:如果由于未选择项而放弃了所有内容,为什么要打开连接并查询数据库?最好在查询之前指定查询的含义?