实现从datatable到datagridview的价值

时间:2018-02-20 20:33:47

标签: c# sql datagridview

我正在尝试根据附加到医生姓名首字母的相应数据库值来更改3个单元格的颜色。换句话说,医生在观察患者时具有细胞背景的特定颜色。数据库“医生”有三列(Physician Name,Initials,Color)。以下是一些代码。

void PhysicianColorTreatPrep()

    {
        string ColorQuery = "SELECT * FROM physicians";

        SqlConnection connectionstring = new SqlConnection(constring);

        connectionstring.Open();

        DataTable dsDocColor = new DataTable();
        SqlDataAdapter adapterDocColor = new SqlDataAdapter(ColorQuery, constring);
        adapterDocColor.Fill(dsDocColor);

        foreach (DataGridViewRow row in datagridviewTreatmentPrep.Rows)
        {
            DataRow[] result = dsDocColor.Select("Color WHERE Initials = "+row.Cells["Primary_Onc"].Value.ToString()+"");

            row.Cells["Last_Name"].Style.BackColor =  //Color retrieved from datatable based on datagridview value
            row.Cells["First_Name"].Style.BackColor = //Color retrieved from datatable based on datagridview value
            row.Cells["Primary_Onc"].Style.BackColor = //Color retrieved from datatable based on datagridview value

        }

    }         

1 个答案:

答案 0 :(得分:0)

@ChetanRanpariya感谢您提出正确的问题。我设法解决了它。以下是代码。

DataRow[] result = dsDocColor.Select("Initials = '"+row.Cells["Primary_Onc"].Value.ToString()+"'");

            string DocColor = result[0][2].ToString();

            row.Cells["Last_Name"].Style.BackColor = System.Drawing.Color.FromName(DocColor);

            row.Cells["First_Name"].Style.BackColor = System.Drawing.Color.FromName(DocColor);
            row.Cells["Primary_Onc"].Style.BackColor = System.Drawing.Color.FromName(DocColor);