简体   繁体   中英

Implement value from datatable into datagridview

I'm trying to change the color of 3 cells based on the corresponding database value attached to the physician initials. In other words, the physician has a specific color for the cell background when they view their patients. The database "physicians" has three columns (Physician Name, Initials, Color). Below is some of the code.

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

        }

    }         

@ChetanRanpariya Thanks for asking the right questions. I managed to solve it. Below is the code.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM