7lrncoxx  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(165)

我有两个datagrid需要显示来自两个表的数据,我为每个datagrid使用了两个load表来显示数据。

public diagnosechipcomplainprint()
{
    InitializeComponent();
    load_table();
}

void load_table()
{
    string query = "Select HospitalRecordNo,concat(Patient_Fname, ' ', Patient_Mname, ' ',Patient_Lname) as 'Patient Name',Age,Gender,DateOfBirth,Email,PatientContact from patientinfo;";

    MySqlConnection con = new MySqlConnection(connection);
    MySqlCommand com = new MySqlCommand(query, con);

    try
    {
        MySqlDataAdapter sd = new MySqlDataAdapter();
        sd.SelectCommand = com;
        DataTable dba = new DataTable();
        sd.Fill(dba);
        BindingSource bs = new BindingSource();
        bs.DataSource = dba;
        dataGridView1.DataSource = bs;
        sd.Update(dba);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

void load_table2()
{
    string query2 = "Select VisitNo, HospitalRecordNo, DateOfVisit from visit_details where HospitalRecordNo = '" + recordno.Text + "';";

    MySqlConnection con = new MySqlConnection(connection);
    MySqlCommand com = new MySqlCommand(query2, con);

    try
    {
        MySqlDataAdapter sd = new MySqlDataAdapter();
        sd.SelectCommand = com;
        DataTable dba = new DataTable();
        sd.Fill(dba);
        BindingSource bs = new BindingSource();
        bs.DataSource = dba;
        dataGridView2.DataSource = bs;
        sd.Update(dba);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

当我点击第一个数据网格时,数据网格中的数据会显示在标签中,但当我点击第二个数据网格时,它会显示错误。

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0)
    {
        DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
        recordno.Text = 
        row.Cells["HospitalRecordNo"].Value.ToString();
        pname.Text = row.Cells["Patient Name"].Value.ToString();
        age.Text = row.Cells["Age"].Value.ToString();
        gender.Text = row.Cells["Gender"].Value.ToString();
        dateofbirth.Text = row.Cells["DateOfBirth"].Value.ToString();
        email.Text = row.Cells["Email"].Value.ToString();
        contact.Text = row.Cells["PatientContact"].Value.ToString();
    }
}

private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0)
    {
        DataGridViewRow row1 = dataGridView2.Rows[e.RowIndex];

        visitno.Text = row1.Cells["VisitNo"].Value.ToString();
        rec.Text = row1.Cells["HospitalRecordNo"].Value.ToString();
        dateofvisit.Text = row1.Cells["DateOfVisit"].Value.ToString();
        nurse.Text = row1.Cells["Nurse_on_duty"].Value.ToString();
        temp.Text = row1.Cells["Temperature"].Value.ToString();
        cardiac.Text = row1.Cells["Cardiac_Rate"].Value.ToString();
        respiratory.Text = 
        row1.Cells["Respiratory_Rate"].Value.ToString();
        bloodpress.Text = 
        row1.Cells["Blood_Pressure"].Value.ToString();
        weight.Text = row1.Cells["Weight"].Value.ToString();
        sat.Text = row1.Cells["02_Stat"].Value.ToString();
    }
}

这就是我在cellclick datagrid视图2中使用的代码。我希望你能帮助我。谢谢您

9udxz4iz

9udxz4iz1#

由于查询未返回名为 Nurse_on_duty . 其他一些列也出现了同样的问题。

nurse.Text = row1.Cells["Nurse_on_duty"].Value.ToString();
yxyvkwin

yxyvkwin2#

我找到了第二个问题 DataGridView 只包含3列定义,实际上需要10列,如中所述 DataGridViewRow.Cells 索引器:

// only 3 columns returned in result set
string query2 = "Select VisitNo, HospitalRecordNo, DateOfVisit from visit_details where HospitalRecordNo = '" + recordno.Text + "';";

您应该提到 DataGridViewRow.Cells 在查询结果集中,使用参数化查询防止sql注入:

string query2 = @"Select VisitNo, HospitalRecordNo, DateOfVisit, Nurse_on_duty, 
                  Temperature, Cardiac_Rate, Respiratory_Rate, Blood_Pressure, 
                  Weight, 02_Stat
                  from visit_details where HospitalRecordNo = @RecordNo";

// MySqlCommand parameter assignment
com.Parameters.AddWithValue("@RecordNo", recordno.Text);

相关问题