使用Checkbox(datagridview)在foreach中循环C#MessageBox [英] C# MessageBox looping in foreach using Checkbox(datagridview)
本文介绍了使用Checkbox(datagridview)在foreach中循环C#MessageBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用datagridview并使用复选框更新行,并且程序有效,但我在Messagebox中遇到问题显示多条消息成功
这是我的代码在datagridview上显示
Hi, im working on a datagridview and update rows using checkbox, and program works, but i have problems in Messagebox showing multiple Message "Success"
Here's my code Displaying on datagridview
MySqlDataAdapter sda = new MySqlDataAdapter("Select * from Foreclosed", con);
DataTable dt = new System.Data.
sda.Fill(dt);
metroGrid1.Rows.Clear();
foreach (DataRow item in dt.Rows)
{
int n = metroGrid1.Rows.Add();
metroGrid1.Rows[n].Cells[0].Value = "false";
metroGrid1.Rows[n].Cells[1].Value = item["Bank_Code"].ToString();
metroGrid1.Rows[n].Cells[2].Value = item["Property_Code"].ToString();
metroGrid1.Rows[n].Cells[3].Value = item["Region"].ToString();
metroGrid1.Rows[n].Cells[4].Value = item["Province"].ToString();
metroGrid1.Rows[n].Cells[5].Value = item["Detailed_Address"].ToString();
对于更新按钮
For the Update Button
foreach (DataGridViewRow item in metroGrid1.Rows)
{
var cell = item.Cells[0];
var value = cell.Value;
if (cell != null)
{
if (value != null && (bool)value == true)
{
con.Open();
MySqlCommand com2 = new MySqlCommand("UPDATE Foreclosed SET Bank_Code=@bankcode WHERE Property_Code='" + item.Cells[2].Value.ToString() + "'", con);
com2.Parameters.AddWithValue("@bankcode", fmupdtxtBox.Text);
com2.ExecuteNonQuery();
con.Close();
}
MessageBox.Show("Success");
}
}
请帮助我如何避免收到循环消息
Kindly help me how to avoid getting a loop message
推荐答案
如果您不希望每次迭代都显示一条消息,只需移动该行
If you don't want a message to be shown for every iteration, just move the line
MessageBox.Show("Success");
foreach
循环。
如果事务成功,那么在循环中放置一个布尔值设置为true你添加一个if语句的循环。
outside the foreach
loop.
Put a boolean inside the loop that you set to true if the transaction is successful and then outside the loop you add an if-statement.
if (succcess)
MessageBox.Show("Success");
这篇关于使用Checkbox(datagridview)在foreach中循环C#MessageBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文