如何在dataGridview内的组合框中获取值并将其放入我的SQL Select查询中?

时间:2019-01-31 06:44:27

标签: c# sql

我想获取组合框调用dcourse的值,以将其放入我的Sql select查询中,但是它不起作用。这是我的代码

     DataTable dt = new DataTable();

     dt.Columns.Add("ID", typeof(int));
     dgEdit.DataSource = dt;

     DataGridViewComboBoxColumn dcourse = new DataGridViewComboBoxColumn();
     dcourse.HeaderText = "Course";
     dcourse.Items.Add("AT (Automotive Technology)");
     dcourse.Items.Add("CPT (Computer Technology)");
     dcourse.Items.Add("ELT (Electrical Technology)");
     dcourse.Items.Add("ICT (Instrumentation and Control Technology)");

     dgEdit.Columns.Add(dcourse);

     DataGridViewComboBoxColumn dname = new DataGridViewComboBoxColumn();
     dname.HeaderText = "Student Name";
     conn.Open();
     cmd = conn.CreateCommand();
     cmd.CommandType = CommandType.Text;
     cmd.CommandText = "Select Name from TBL_Student WHERE Course = '" + 
     dgEdit.CurrentRow.Cells[1].Value + "' ";
     DataTable dat = new DataTable();
     SqlDataAdapter dap = new SqlDataAdapter(cmd);
     dap.Fill(dat);
     foreach (DataRow dr in dat.Rows)
     {
        dname.Items.Add(dr["Name"].ToString());
     }
     conn.Close();
     dgEdit.Columns.Add(dname);

我得到了这个弹出窗口,并且“ dgEdit.CurrentRow.Cells 1。Value”应该是课程

this is the error

0 个答案:

没有答案