找不到名为列的列

时间:2018-08-15 02:40:33

标签: c# mysql

我有2个datagrid,需要显示2个表中的数据,我对每个datagrid使用2个加载表来显示数据。

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

当我在单元格中单击第一个数据网格时,datagrid中的数据将显示在标签中,但是当我单击第二个数据网格时,它将显示错误。

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数据网格视图2中使用的代码。 我希望你能帮助我。谢谢

2 个答案:

答案 0 :(得分:0)

由于查询未返回名为Nurse_on_duty的列,因此在运行时收到错误。 其他一些列也发生相同的问题。

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

答案 1 :(得分:0)

我发现对第二个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所需的所有列名,并加上parameterized query来防止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);