2017-01-16 25 views
0

我想添加数据但我想把它放在两个DataGridViews中。我试过,但在第二次的DataGridView我不会工作,它不会添加,只有在第一次的DataGridView将数据添加到两个Datagridviews

string query = "insert into dbuser.patientform (patientname,homeaddress,occupation,emailaddress,sex,age,status,birthday,cellphoneno,refferedby,date,diagnosis,treatment)" 
    + "values('" + this.textBox1.Text + "','" + this.textBox2.Text + "','" + this.textBox3.Text + "','" + this.textBox4.Text + "','" + this.comboBox1.SelectedItem + "','" + this.textBox5.Text + "','" + this.comboBox2.SelectedItem + "','" + this.dateTimePicker1.Text + "','" + this.textBox6.Text + "','" + this.textBox7.Text + "','" + this.dateTimePicker2.Text + "','" + this.textBox8.Text + "','" + this.textBox9.Text + "') ;"; 

string quer = "insert into dbuser.patienthistory (patientname,date,diagnosis,treatment)" 
+ "values('" + this.textBox1.Text + this.dateTimePicker2.Text + "','" + this.textBox8.Text + "','" + this.textBox9.Text + "') ;"; 


MySqlCommand cmd = new MySqlCommand(query, condb); 
MySqlCommand cm = new MySqlCommand(quer, condb); 

MySqlDataReader myreader; 
MySqlDataReader myreader1; 
try 
{ 
    condb.Open(); 
    myreader = cmd.ExecuteReader(); 
    condb.Close(); 

    condb.Open(); 
    myreader1 = cm.ExecuteReader(); 


    MessageBox.Show("Saved"); 
    textBox1.Clear(); 
    textBox2.Clear(); 
    textBox3.Clear(); 
    textBox4.Clear(); 
    comboBox1.SelectedItem = false; 
    textBox5.Clear(); 
    comboBox2.SelectedItem = false; 
    textBox6.Clear(); 
    textBox7.Clear(); 
    textBox8.Clear(); 
    textBox9.Clear(); 
    dateTimePicker1.Text = null; 

    condb.Close(); 


    dataset = new DataTable(); 
    da.Fill(dataset); 
    BindingSource bs = new BindingSource(); 

    bs.DataSource = dataset; 
    dataGridView1.DataSource = bs; 
    dataGridView2.DataSource = bs; 

    da.Update(dataset); 
+0

通过路在何方'da'值初始化? –

回答

0

我建议做如下:

  1. 如果打开和关闭连接一个两个时间请求将使用相同的连接字符串

  2. 使用MySqlDataAdapter将数据填充到DataTable。

  3. 集数据表到DataGridView的

+0

谢谢,我会试试这:) –