我想将与checkedListbox中所选项目相关的所有值添加到Textbox中。要从数据库中检索的值。我创建了包含两列place和cost的表。我用表中的位置填充了checkedlistbox,我希望根据表中所选项的相关成本,在checkedlistbox下面的Textbox中获得总成本的结果。
数据库表
放置成本
海德拉巴3000.00
bombay 2000.00
banglore 5000.00
chennai 7000.00
private void bill_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'testdbDataSet.customer' table. You can move, or remove it, as needed.
this.customerTableAdapter.Fill(this.testdbDataSet.customer);
SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings["conn"]);
cn.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from place", cn);
DataSet ds = new DataSet();
da.Fill(ds);
foreach(DataRow dr in ds.Tables[0].Rows)
{
checkedListBox1.Items.Add(dr.ItemArray[0]);
}
答案 0 :(得分:0)
我不知道这应该被称为优雅还是混乱......
string places = String.Join("','", checkedListBox.CheckedItems.OfType<string>().ToList());
string query = String.Format("SELECT SUM(cost) FROM yourTable WHERE places IN ('{0}')", places);
将查询传递给您的数据库,您应该得到您的总和。