找不到名为的列

我有 2 个数据网格需要显示 2 个表中的数据,我为每个数据网格使用了 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);

    }

}

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

这是我在 Cellclick 数据网格视图 2 中使用的代码。我希望你能帮助我。谢谢


蛊毒传说
浏览 366回答 2
2回答

aluckdog

我发现第二个查询DataGridView只包含 3 个列定义,实际上您需要 10 个列,如DataGridViewRow.Cells索引器中所述:// only 3 columns returned in result setstring 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 assignmentcom.Parameters.AddWithValue("@RecordNo", recordno.Text);

慕丝7291255

您在运行时收到错误,因为您的查询未返回名为Nurse_on_duty. 其他一些列也发生了同样的问题。nurse.Text = row1.Cells["Nurse_on_duty"].Value.ToString();
打开App,查看更多内容
随时随地看视频慕课网APP