zoukankan      html  css  js  c++  java
  • 突出显示某个单元格内容

    要求:如果某个员工的起薪大于20000,就用红色的背景颜色突出显示:

    前台代码:

    View Code
    1 <asp:GridView ID="GridView1" ShowFooter="true" runat="server" AutoGenerateColumns ="false"
    2 style="border:solid 1px black" onrowdatabound="GridView1_RowDataBound">
    3 <Columns>
    4 <asp:BoundField DataField="编号" HeaderText ="编号" />
    5 <asp:BoundField DataField ="姓名" HeaderText ="姓名" />
    6 <asp:BoundField DataField ="邮政编码" HeaderText ="邮政编码" />
    7 <asp:BoundField DataField ="家庭住址" HeaderText ="家庭住址" />
    8 <asp:TemplateField HeaderText ="出生年月">
    9 <ItemTemplate>
    10 <%#Eval("出生年月","{0:yyyy-MM-dd}")%>
    11 </ItemTemplate>
    12 </asp:TemplateField>
    13 <asp:BoundField DataField ="起薪" HeaderText ="起薪" />
    14 </Columns>
    15 <HeaderStyle Font-Bold="true" BackColor="#006699" ForeColor="White" />
    16 </asp:GridView>

    后台代码:

    View Code
    1 protected void Page_Load(object sender, EventArgs e)
    2 {
    3 if (!IsPostBack)
    4 {
    5 databind();
    6 }
    7
    8 }
    9 public void databind()
    10 {
    11 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
    12 SqlCommand cmd = new SqlCommand();
    13 cmd.Connection = con;
    14 cmd.CommandText = "select * from T_Message";
    15 SqlDataAdapter da = new SqlDataAdapter(cmd);
    16 DataSet ds = new DataSet();
    17 da.Fill(ds);
    18 this.GridView1.DataSource = ds.Tables[0];
    19 this.GridView1.DataBind();
    20 }
    21 double sum = 0;
    22 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    23 {
    24 foreach (TableCell item in e.Row.Cells)
    25 {
    26 item.Attributes.Add("style", "border-color:black");
    27 }
    28
    29 if (e.Row.RowType == DataControlRowType.DataRow)
    30 {
    31 sum = sum + Convert.ToInt32(e.Row.Cells[5].Text);
    32 if (Convert.ToInt32(e.Row.Cells[5].Text) > 20000)
    33 {
    34 e.Row.Cells[5].BackColor = Color.Red;
    35 }
    36 }
    37 if (e.Row.RowType == DataControlRowType.Footer)
    38 {
    39 e.Row.Cells[4].Text = "每月支出薪水";
    40 e.Row.Cells[2].Text = "平均薪水";
    41 e.Row.Cells[3].Text = (sum / GridView1.Rows.Count).ToString();
    42 e.Row.Cells[5].Text = sum.ToString();
    43 }
    44 }
    怀揣着一点点梦想的年轻人
    相信技术和创新的力量
    喜欢快速反应的工作节奏
  • 相关阅读:
    EasyUI左边树菜单和datagrid分页
    Linux上安装Redis教程
    TreeMap和TreeSet的区别与联系
    将Map<String, List<Map<String,Object>>>进行排序
    Linux系统安装JDK和Tomcat
    点击添加按钮,使用ajax动态添加一行和移除一行,并且序号重新排序和数据不重复操作判断
    23种设计模式汇总整理
    SSH架构BaseDao实现
    双击Table表格td变成text修改内容
    用户找回密码功能JS验证邮箱通过点击下一步隐藏邮箱输入框并修改下一步按钮的ID
  • 原文地址:https://www.cnblogs.com/hfliyi/p/1982754.html
Copyright © 2011-2022 走看看