根据条件动态改变GridView某行或某个单元格的背景色

C#代码
  1. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
  2.     {  
  3.         // 设置行的背景颜色  
  4.         if (e.Row.Cells[1].Text == "0111")  
  5.         {  
  6.             e.Row.BackColor = System.Drawing.Color.Red;  
  7.         }  
  8.         else  
  9.         {  
  10.             e.Row.BackColor = System.Drawing.Color.Gray;  
  11.         }  
  12.         // 设置单元格的颜色  
  13.         if (e.Row.Cells[1].Text == "0111")  
  14.         {  
  15.             e.Row.Cells[1].BackColor = System.Drawing.Color.Green;  
  16.         }  
  17.       }
  18.  
  19.  
  20.  
    C#代码
    1. 21. //其实不一定用上面的方面,以下为我的实际应用  
    2. 22. public void bind()  
    3.         {  
    4.             string sqlstr = "select a.id,b.department,b.name,c.pname,a.wrktime_start,a.wrktime_end,a.check_time,a.Confirmed_add_time,(select CODE_VALUES from jsboth_hrms_codes where code_num=a.add_type and code_type='WOT_TYPE') as add_type,a.add_apply_sid,a.absense_late,a.absense_early,a.absense_time,(select CODE_VALUES from jsboth_hrms_codes where code_num=a.absense_type and code_type='vocation') as absense_type,a.ABSENSE_SID,a.remarks,pm_checked from jsboth_hrms_dailycheck a,jsboth_hrms_baseinfo b,jsboth_hrms_projectinfo c where a.emp_id=b.id and a.project_id=c.id";  
    5.             using (SqlConnection conn = new SqlConnection(strCon))  
    6.             {  
    7.                 conn.Open();  
    8.                 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, conn);  
    9.                 DataSet myds = new DataSet();  
    10.                 DropDownList ddlAddType;  
    11.                 DropDownList ddlAbsenseType;  
    12.   
    13.                 //myda.Fill(myds,"JSBOTH_HRMS_DAILYCHECK");  
    14.                 myda.Fill(myds, "jsboth_hrms_dailycheck");  
    15.                 GridView1.DataSource = myds;  
    16.                 GridView1.DataKeyNames = new string[] { "id" };//主键  
    17.                 GridView1.DataBind();  
    18.   
    19.                 //GridView1.Rows[k].Cells[19].Visible = false;  
    20.   
    21.                 string sqlGetAllDailyData = "select * from jsboth_hrms_dailycheck";  
    22.                 SqlDataAdapter getAllDailyDataDa = new SqlDataAdapter(sqlGetAllDailyData, conn);  
    23.                 DataTable allDailyDt = new DataTable();  
    24.                 getAllDailyDataDa.Fill(allDailyDt);  
    25.   
    26.                 for (int i = 0; i < GridView1.Rows.Count; i++)  
    27.                 {  
    28.                     DataRowView dailyCheckRow = myds.Tables["jsboth_hrms_dailycheck"].DefaultView[i];  
    29.   
    30.                     //Response.Write("ADD_TYPE=" + dailyCheckRow["ADD_TYPE"].ToString());  
    31.   
    32.                    
    33.                     if (Convert.ToString(dailyCheckRow["PM_CHECKED"]) == "0") {  
    34.                      
    35.                     //GridView1.Rows[i].Cells[3].  
    36.                         GridView1.Rows[i].Cells[3].BackColor = System.Drawing.Color.Red;  
    37.                     }  
    38.   
    39.   
    40.                     if ((GridView1.Rows[i].RowState & DataControlRowState.Edit) == DataControlRowState.Edit)  
    41.                     {  
    42.                     if (Convert.ToString(dailyCheckRow["ADD_TYPE"]) == "普通")  
    43.                     {  
    44.                         //Response.Write("000000000000000000000");  
    45.                         ddlAddType = (DropDownList)GridView1.Rows[i].FindControl("gvEditDDLAddType");  
    46.                         ddlAddType.SelectedIndex = 0;  
    47.                     }  
    48.                     if (Convert.ToString(dailyCheckRow["ADD_TYPE"]) == "节假日")  
    49.                     {  
    50.                         ddlAddType = (DropDownList)GridView1.Rows[i].FindControl("gvEditDDLAddType");  
    51.                         ddlAddType.SelectedIndex = 1;  
    52.                     }  
    53.   
    54.                     //FOR GRIDVIEW DDLABSENSETYPE  
    55.                     if (string.IsNullOrEmpty(Convert.ToString(dailyCheckRow["ABSENSE_TYPE"])))  
    56.                     {  
    57.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvEditDDLAbsenseType");  
    58.                         ddlAbsenseType.SelectedIndex = 0;  
    59.   
    60.                     }  
    61.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "病假")  
    62.                     {  
    63.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvEditDDLAbsenseType");  
    64.                         ddlAbsenseType.SelectedIndex = 1;  
    65.                     }  
    66.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "事假")  
    67.                     {  
    68.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvEditDDLAbsenseType");  
    69.                         ddlAbsenseType.SelectedIndex = 2;  
    70.                     }  
    71.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "调休")  
    72.                     {  
    73.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvEditDDLAbsenseType");  
    74.                         ddlAbsenseType.SelectedIndex = 3;  
    75.                     }  
    76.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "婚假")  
    77.                     {  
    78.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvEditDDLAbsenseType");  
    79.                         ddlAbsenseType.SelectedIndex = 4;  
    80.                     }  
    81.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "丧假")  
    82.                     {  
    83.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvEditDDLAbsenseType");  
    84.                         ddlAbsenseType.SelectedIndex = 5;  
    85.                     }  
    86.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "产假")  
    87.                     {  
    88.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvEditDDLAbsenseType");  
    89.                         ddlAbsenseType.SelectedIndex = 6;  
    90.                     }  
    91.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "探亲假")  
    92.                     {  
    93.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvEditDDLAbsenseType");  
    94.                         ddlAbsenseType.SelectedIndex = 7;  
    95.                     }  
    96.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "带薪假")  
    97.                     {  
    98.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvEditDDLAbsenseType");  
    99.                         ddlAbsenseType.SelectedIndex = 8;  
    100.                     }  
    101.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "年假")  
    102.                     {  
    103.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvEditDDLAbsenseType");  
    104.                         ddlAbsenseType.SelectedIndex = 9;  
    105.                     }  
    106.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "公差")  
    107.                     {  
    108.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvEditDDLAbsenseType");  
    109.                         ddlAbsenseType.SelectedIndex = 10;  
    110.                     }  
    111.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "路途")  
    112.                     {  
    113.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvEditDDLAbsenseType");  
    114.                         ddlAbsenseType.SelectedIndex = 11;  
    115.                     }  
    116.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "旷工")  
    117.                     {  
    118.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvEditDDLAbsenseType");  
    119.                         ddlAbsenseType.SelectedIndex = 12;  
    120.                     }  
    121.                 }  
    122.                 else {  
    123.                     if (Convert.ToString(dailyCheckRow["ADD_TYPE"]) == "普通")  
    124.                     {  
    125.                         //Response.Write("000000000000000000000");  
    126.                         ddlAddType = (DropDownList)GridView1.Rows[i].FindControl("gvDDLAddType");  
    127.                         ddlAddType.SelectedIndex = 0;  
    128.                     }  
    129.                     if (Convert.ToString(dailyCheckRow["ADD_TYPE"]) == "节假日")  
    130.                     {  
    131.                         ddlAddType = (DropDownList)GridView1.Rows[i].FindControl("gvDDLAddType");  
    132.                         ddlAddType.SelectedIndex = 1;  
    133.                     }  
    134.   
    135.                     //FOR GRIDVIEW DDLABSENSETYPE  
    136.                     if (string.IsNullOrEmpty(Convert.ToString(dailyCheckRow["ABSENSE_TYPE"])))  
    137.                     {  
    138.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvDDLAbsenseType");  
    139.                         ddlAbsenseType.SelectedIndex = 0;  
    140.   
    141.                     }  
    142.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "病假")  
    143.                     {  
    144.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvDDLAbsenseType");  
    145.                         ddlAbsenseType.SelectedIndex = 1;  
    146.                     }  
    147.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "事假")  
    148.                     {  
    149.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvDDLAbsenseType");  
    150.                         ddlAbsenseType.SelectedIndex = 2;  
    151.                     }  
    152.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "调休")  
    153.                     {  
    154.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvDDLAbsenseType");  
    155.                         ddlAbsenseType.SelectedIndex = 3;  
    156.                     }  
    157.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "婚假")  
    158.                     {  
    159.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvDDLAbsenseType");  
    160.                         ddlAbsenseType.SelectedIndex = 4;  
    161.                     }  
    162.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "丧假")  
    163.                     {  
    164.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvDDLAbsenseType");  
    165.                         ddlAbsenseType.SelectedIndex = 5;  
    166.                     }  
    167.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "产假")  
    168.                     {  
    169.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvDDLAbsenseType");  
    170.                         ddlAbsenseType.SelectedIndex = 6;  
    171.                     }  
    172.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "探亲假")  
    173.                     {  
    174.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvDDLAbsenseType");  
    175.                         ddlAbsenseType.SelectedIndex = 7;  
    176.                     }  
    177.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "带薪假")  
    178.                     {  
    179.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvDDLAbsenseType");  
    180.                         ddlAbsenseType.SelectedIndex = 8;  
    181.                     }  
    182.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "年假")  
    183.                     {  
    184.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvDDLAbsenseType");  
    185.                         ddlAbsenseType.SelectedIndex = 9;  
    186.                     }  
    187.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "公差")  
    188.                     {  
    189.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvDDLAbsenseType");  
    190.                         ddlAbsenseType.SelectedIndex = 10;  
    191.                     }  
    192.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "路途")  
    193.                     {  
    194.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvDDLAbsenseType");  
    195.                         ddlAbsenseType.SelectedIndex = 11;  
    196.                     }  
    197.                     if (Convert.ToString(dailyCheckRow["ABSENSE_TYPE"]) == "旷工")  
    198.                     {  
    199.                         ddlAbsenseType = (DropDownList)GridView1.Rows[i].FindControl("gvDDLAbsenseType");  
    200.                         ddlAbsenseType.SelectedIndex = 12;  
    201.                     }  
    202.                      
    203.                     }  
    204.                 }  
    205.             }  
    206.   
    207.         }  
原文地址:https://www.cnblogs.com/sdjxcolin/p/1414018.html