Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

C#
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

C#
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
Posted
Updated 1-Dec-15 16:15pm
v8

1 solution

If you don't want a message to be shown for every iteration, just move the line
C#
MessageBox.Show("Success");

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.
C#
if (succcess)
    MessageBox.Show("Success");
 
Share this answer
 
Comments
Rencyrence 1-Dec-15 23:52pm    
Hello, it works, but i tried to play with checkbox, i tried to remove the check from checkbox and try to click Update Button, but the message was shown.
Rencyrence 2-Dec-15 0:03am    
Hello again, i already fix it, by using try finally, and set to default success=false; im wondering now how will i catch if there's no more selected checkbox, and display a message, tell me if i need to create another thread, thanks
Rencyrence 2-Dec-15 0:42am    
I got it already , thanks
George Jonsson 2-Dec-15 0:44am    
Sometimes it helps to do it yourself :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900