如何在运行时使用存储过程填充Datagridview组合框,其中where where condition from database

时间:2018-04-06 15:41:09

标签: c# datagridview combobox

我有以下设计和数据库表:

1- machine表,其中包含Machine_id,Machine_name。

2- LAB_RESULTS表:

SELECT [ID]
      ,[SAMPLE_ID]
      ,[ORDER_ID]
      ,[TESTID]
      ,[GROUPID]
      ,[NORMAL_RESULT]
      ,[SAMPLE_STATUS]
      ,[EXAMINED_BY]
      ,[EXAMINED_DATE]
      ,[APPROVED_BY]
      ,[APPROVED_DATE]
      ,[RESULT_NUMBER]
      ,[RESULT_REPORT]
      ,[RESULT_NOTE]
      ,[packageid]
      ,[machine_id]
      ,[deptid]
      ,[patient_no]
      ,[custid]
      ,[REQ_FORM_NO]
      ,[serial]
      ,[text]
      ,[value]
  FROM LAB_RESULTS

3-我正在保存测试结果以及该测试完成的机器。

4-我想创建更新窗口并检索测试信息和机器名称以使用datagridview更新机器名称并在组合框中填充机器名称。

5-我创建了以下存储过程:

ALTER proc [dbo].[GET_MACHINES_FOR_RESULT]
@ORDER_ID int,
@TESTID int
AS 
SELECT Machines.Machine_id,Machine_name FROM Machines
inner join LAB_RESULTS on LAB_RESULTS.machine_id = Machines.Machine_id
where Machines.Machine_id = LAB_RESULTS.machine_id
and LAB_RESULTS.ORDER_ID = @ORDER_ID
and LAB_RESULTS.TESTID = @TESTID 

6-在c#应用程序窗口中,我创建了以下公共数据表:

public DataTable GET_MACHINES_FOR_RESULT(int ORDER_ID , int TESTID)
        {
            DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
            DataTable dt = new DataTable();

            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@ORDER_ID", SqlDbType.Int);
            param[0].Value = ORDER_ID;

            param[1] = new SqlParameter("@TESTID", SqlDbType.Int);
            param[1].Value = TESTID;

            dt = DAL.SelectData("GET_MACHINES_FOR_RESULT", param);
            DAL.close();
            return dt;
        }

7-最后一步我创建了组合框列,并在选择ORDER_NO后使用此代码填充组合框和数据视图,但我得到的错误输入字符串格式不正确。

dgvResult.DataSource = result.GetChemForApprove(txtOrder.Text);
            DataGridViewComboBoxColumn cmbmachine = new DataGridViewComboBoxColumn();
            cmbmachine.HeaderText = "Machine Name";
            cmbmachine.Name = "machine";
            for (int i = 0; i < dgvResult.Rows.Count; i++)
            {
                cmbmachine.DataSource = result.GET_MACHINES_FOR_RESULT(Convert.ToInt32(txtOrder.Text), Convert.ToInt32(dgvResult.Rows[i].Cells[2].ToString())); // Bind combobox with datasource.  
            }

            cmbmachine.ValueMember = "Machine_id";
            cmbmachine.DisplayMember = "Machine_name";
            dgvResult.Columns.Add(cmbmachine);

GetChemForApprove存储过程将使用订单信息填充datagridview。

如何更改代码以在组合框中读取机器名称。

对不起,如果问题很大,但我试图添加所有细节,以清除一切。

编辑:我只是在LAB_RESULTS表中保存testid + machine_id,我需要创建另一个窗口来更新machine_name,例如表lab_results包含以下顺序:

order_id   testid  machine_id 
  1          1        4
  1          2        4
  1          3        2
  1          4        2 
当我选择订单号1时,我需要在新窗口中看到我需要阅读的内容 testid +机器名称。 我想在组合框列中获取机器名称,但显示保存的机器名称,我想将机器名称更新为另一个机器名称。 这个想法是正确的还是给我另一种方法来实现这一点。

0 个答案:

没有答案
相关问题