将数据添加到两个Datagridviews

时间:2017-01-16 11:37:46

标签: c# visual-studio-2012

我想添加数据,但我想把它放在2个DataGridViews中。我试过这个,但在第二个Datagridview我不会工作,它不会添加,只在第一个Datagridview

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);

1 个答案:

答案 0 :(得分:0)

我建议做以下事项:

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

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

  3. 将DataTable设置为DataGridView