为了账号安全,请及时绑定邮箱和手机立即绑定

找不到名为的列

找不到名为的列

C#
蛊毒传说 2021-11-28 18:46:58
我有 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 中使用的代码。我希望你能帮助我。谢谢
查看完整描述

2 回答

?
aluckdog

TA贡献1847条经验 获得超7个赞

我发现第二个查询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);


查看完整回答
反对 回复 2021-11-28
?
慕丝7291255

TA贡献1859条经验 获得超6个赞

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

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


查看完整回答
反对 回复 2021-11-28
  • 2 回答
  • 0 关注
  • 365 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信