Click here to Skip to main content
15,886,597 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,all my friends!

When i write program,i run into two problems!
1.After click "Update" button,i click "Show" button,the datagridview will not display data.
2.The second click "Update" button,message box show:"Violation of Concurrency: UpdateCommand affects the expected 1 records in 0".

I don't know how to solution the problems!
I think i need refresh datagridview!

There are a DataGridView control and two Button controls in my program.
following is code.

Form1_Load:
C#
private void Form1_Load(object sender, EventArgs e)
{
    LoadDataGridView_RobotVersion();
}

The "Show" button:
C#
private void button_show_Click(object sender, EventArgs e)
{            
     LoadDataGridView_RobotVersion();      
}

C#
private void LoadDataGridView_RobotVersion()
{            
    this.dataGridView_robotversion.DataSource = null;
            
    setDgStyle_robotversion();

    DataTable robotversion = new DataTable();

    switch (this.comboBox_all_process.SelectedIndex)
    {
        case 0:
              robotversion = dbconn("select STT,Process,Version,Path from tblversion_tstc");
              break;
        case 1:
              robotversion = dbconn("select STT,Process,Version,Path from tblversion_tstc where Process='RFLCA'");
               break;
        case 2:
              robotversion = dbconn("select STT,Process,Version,Path from tblversion_tstc where Process='FINAL'");
                    break;
        case 3:
             robotversion = dbconn("select STT,Process,Version,Path from tblversion_tstc where Process='IMEI'");
             break;
        default:
             robotversion = dbconn("select STT,Process,Version,Path from tblversion_tstc");
             break;
     }            
     this.dataGridView_robotversion.DataSource = robotversion;
}

The "Update" button:
C#
private void button_update_Click(object sender, EventArgs e)
{          
    string strSql = "select STT,Process,Version,Path from tblversion_tstc";
    System.Data.DataTable dtUpdate = new System.Data.DataTable();
    dtUpdate = this.dbconn(strSql);
    dtUpdate.Rows.Clear();

    System.Data.DataTable dtShow = new System.Data.DataTable();

    dtShow = (System.Data.DataTable)this.dataGridView_robotversion.DataSource;

    int p1 = dtShow.Rows.Count;           

    for (int i = 0; i < dtShow.Rows.Count; i++)
    {
        dtUpdate.ImportRow(dtShow.Rows[i]);
    }
    int num = dtUpdate.Rows.Count;
    try
    {                
         this.conn.Open();

         trans = this.conn.BeginTransaction();

         MySqlCommandBuilder CommandBuiler;
         CommandBuiler = new MySqlCommandBuilder(this.adapter);

         this.adapter.Update(dtUpdate);
         trans.Commit();
         his.conn.Close();

     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
         trans.Rollback();
         return;
     }

     dtUpdate.AcceptChanges();
}

C#
 private System.Data.DataTable dbconn(string strSql)
{
     DataTable dtSelect = new DataTable();
           
     try
     {
         conn = new MySqlConnection();
         conn.ConnectionString = MyConnectionString;
         conn.Open();
         this.adapter = new MySqlDataAdapter(strSql, conn);
         int rnt = this.adapter.Fill(dtSelect);
      }
      catch(Exception ex)
      {
         MessageBox.Show(ex.ToString());
      }
      finally
      {
         conn.Close();
      }
            
      return dtSelect;
}
Posted

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