我试图用数据库值在列表框中添加项目,并尝试使用双击事件将其删除,我想删除列表框中的项目,该项目的价格将扣除到 txtTotal.Text。例如。我在列表框名称“Package1”中添加一个价格为 299 的项目,并添加另一个价格为 300 的项目名称“Package2”,如果我删除“Package1”,txtTotal 将减少 299 的数字,它将变为 300。
这是我的代码:
void fillCombo()
{
string constring = "server=localhost;port=3306;username=root;password=root";
string Query = "Select * from dbinfo.tbladvance;";
MySqlConnection con = new MySqlConnection(constring);
MySqlCommand cmdDB = new MySqlCommand(Query, con);
MySqlDataReader myReader;
try
{
con.Open();
myReader = cmdDB.ExecuteReader();
while (myReader.Read())
{
string sName = myReader.GetString("Names");
comboBox1.Items.Add(sName);
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string constring = "server=localhost;port=3306;username=root;password=root";
string Query = "Select * from dbinfo.tbladvance where Names='" + comboBox1.Text + "' ;";
MySqlConnection con = new MySqlConnection(constring);
MySqlCommand cmdDB = new MySqlCommand(Query, con);
MySqlDataReader myReader;
try
{
con.Open();
myReader = cmdDB.ExecuteReader();
while (myReader.Read())
{
double sPrice = myReader.GetDouble("Price");
string sDesc = myReader.GetString("Description");
txtPrice.Text = sPrice.ToString();
txtDesc.Text = sDesc;
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
int total;
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(comboBox1.Text);
int num1 = int.Parse(txtPrice.Text);
int num2 = int.Parse(txtTotal.Text);
total = num1 + num2;
txtTotal.Text = total.ToString();
}
private void listBox1_DoubleClick(object sender, EventArgs e)
{
string constring = "server=localhost;port=3306;username=root;password=root";
string Query = "Select * from dbinfo.tbladvance;";
MySqlConnection con = new MySqlConnection(constring);
MySqlCommand cmdDB = new MySqlCommand(Query, con);
MySqlDataReader myReader;
con.Open();
myReader = cmdDB.ExecuteReader();
while (myReader.Read())
{
for (int n = listBox1.Items.Count - 1; n >= 0; --n)
{
string sName = myReader.GetString("Names");
string removelistitem = sName;
if (listBox1.Items[n].ToString().Contains(removelistitem))
{
listBox1.Items.RemoveAt(n);
}
}
}
con.Close();
}
顺便说一下,我正在使用 mysql,提前致谢:)
我希望我可以解释您提供的代码中的一些兴趣点。有几件事不一定是错误的,但不必要且容易出错。我将从每个不必要的代码片段的严重性开始指出这些。
首先,请看下面的几行……
string constring = "server=localhost;port=3306;username=root;password=root";
string Query = "Select * from dbinfo.tbladvance;";
MySqlConnection con = new MySqlConnection(constring);
MySqlCommand cmdDB = new MySqlCommand(Query, con);
MySqlDataReader myReader;
上面的代码在fillCombo
方法中。在这里,您似乎正在连接并查询(DataTable
我从名称中猜测dbinfo.tbladvance
)一个数据库,并使用该DataTable
查询中的 用来自名为“Names”的列中的字符串填充组合框。然后连接关闭。
这很好,因为您会假设您只需要执行一次。现在让我们转到代码中的下一个方法comboBox1_SelectedIndexChanged
。每次组合框选择更改时都会触发此事件方法。
string constring = "server=localhost;port=3306;username=root;password=root";
string Query = "Select * from dbinfo.tbladvance where Names='" + comboBox1.Text + "' ;";
MySqlConnection con = new MySqlConnection(constring);
MySqlCommand cmdDB = new MySqlCommand(Query, con);
MySqlDataReader myReader;
在这里,您似乎再次打开了与数据库的连接。这次查询是获取与comboBox1.Text
数据表中名为“Names”的列中的字符串匹配的匹配项。您这样做的原因是您需要获取所选套餐的价格和描述。从数据库返回信息后,这些值将放入相应的文本框中。然后关闭连接并退出。
我对此可能是错的,但是,该comboBox1_SelectedIndexChanged
方法可能存在的问题是,每次用户更改组合框选择时,它都会打开和关闭数据库。这是不必要的,因为组合框中的值列表永远不会改变。
代码需要不断查询数据库的原因是代码从不保存第一个查询。查看该fillCombo()
方法,您将看到该代码正在从名称为 的列中获取此数据Names
。查看该comboBox1_SelectedIndexChanged
方法,该数据库中似乎还有另外两列名为:Price
和Description
。假设这些数据在用户使用程序时是静态的,那么将数据(名称、价格、描述)保存到 aDataTable
不仅会使事情变得更容易,而且会将对数据库的查询数量减少到一 (1)。
如果数据实时更改并且用户需要实时访问这些新信息,那么您可能需要考虑不同的方法,但是从提供的代码来看,情况似乎并非如此。
上述长篇大论的要点是您应该考虑将所需的数据读入DataTable
. 然后使用它DataTable
来执行您的查询。这将消除不断打开和关闭数据库的需要,并使代码整洁。
第二:总成本:按钮点击方法看起来很神秘,显然行不通。按照代码,当用户按下按钮时,组合框中当前选定的项目将被放入列表框中。然后进行了某种奇怪的总计,但根据您的描述,您似乎想要使用 .csvtxtTotal
文件中列出的所有“包”的总量更新文本框listBox1
。
如果这是正确的,那么您将不得不使用一个变量来跟踪这个总数,并且看起来您有一个名为total
. 每当将项目添加到列表或删除时,都需要更新并重新显示此变量。处理这个全局变量并不困难,但确实没有必要。由于总价取决于列表中的项目,因此创建一个循环遍历列表并返回列表框中所有项目的总价的方法可能会更容易。这将消除对全局total
变量的需要,并且在添加或删除列表框中的项目时只需一行代码。示例:下面的代码循环遍历列表框中的所有项目并返回列表中所有项目的总和。请记住,下面的代码使用了DataTable
PackagesData
如上所述,其中第 1 列是“包装”,第 2 列是“价格”,第 3 列是“描述”,所有列都是字符串。
private double GetTotal() {
double grandTotal = 0;
double curTotal = 0;
foreach (string curDesc in listBox1.Items) {
foreach (DataRow curRow in PackagesData.Rows) {
if (curRow.ItemArray[2] != null && curRow.ItemArray[1] != null) {
if (curDesc.Equals(curRow.ItemArray[2].ToString())) {
double.TryParse(curRow.ItemArray[1].ToString(), out curTotal);
grandTotal += curTotal;
break;
}
}
}
}
return grandTotal;
}
第三:双击列表框中的项目将其删除并更新总数。这再次使用 aDataTable
和上述方法。
private void listBox1_DoubleClick(object sender, EventArgs e) {
int selectedIndex = listBox1.SelectedIndex;
if (selectedIndex >= 0) {
listBox1.Items.RemoveAt(selectedIndex);
txtTotal.Text = GetTotal().ToString();
}
}
我希望这是有道理的并有所帮助。如果你用你想要的列制作数据表,它应该整理当前的方法并使事情变得更容易。你也可以考虑制作一个“Package”类。祝你好运
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句