zoukankan      html  css  js  c++  java
  • GridView中绑定数据字段时可做的几种操作方法

          在从数据库中取值绑定到 Gridview 时,在数据库中可能某些字段是数字或是一些标志位,在把它们绑定到GridView 后也会显示到数字,例如 IS_SUBMIT 字段,标志某条数据是否已被提交
          "Y"      已提交
          "N"      未提交

          如果需要在值为 "Y" 时显示“已提交”,而在值为 "N" 时显示为“未提交”该怎么办呢?有以下几种方法

    1. 在数据库中做手脚,利用 Case..........When

          SELECT 
          (CASE RoleValueID
                WHEN 'Y' THEN '已提交'
                WHEN 'N' THEN '未提交'
                ELSE '未知'
          END) AS RoleName
          FROM UserRole

          这种方法很常用,对程序也不会造成很大影响,可是如果在程序上的实体层所对应构造函数的数据类型不是字符类型的话(一般往往可能是int),那这里就会出现数据类型不对应的问题。

    2. 使用gridview中的模板配合gridview中的相关事件

          <asp:Button   ID= "Button2 "   runat= "server "   CommandArgument= ' <%#   Eval( "id ")   %> '   CommandName= "IsPass "   Text= ' <%#   Eval( "IS_SUBMIT").ToString()   ==   "Y"   ?   "已提交 "   :   "未提交 "   %> ' />

    3. RowDataBound事件

          if (e.Row.RowType == DataControlRowType.DataRow)
          {
                switch (e.Row.Cells[3].Text.Trim())
                {
                      case "Y":
                            e.Row.Cells[3].Text = "已提交";
                            break;
                      case "N":
                            e.Row.Cells[3].Text = "未提交";
                            break;
                }
          }

    利用RowDataBound还可以改变gridview很多东西

          1.实现字体颜色改变

          前台用超链接列 
          <asp:HyperLinkField DataNavigateUrlFormatString="Detail.aspx" Text="点击查看"> 
          </asp:HyperLinkField>

          后台:

          protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e) 
          { 
                if (e.Row.RowType == DataControlRowType.DataRow) 
                { 
                      HyperLink hlf = (HyperLink)e.Row.Cells[1].Controls[0]; 
                      if (Convert.ToDateTime(gridview1.DataKeys[e.Row.RowIndex].Value.ToString()) > =   DateTime.Now) 
                      { 
                            hlf.ForeColor= System.Drawing.Color.Red;// change color 
                      }

                } 
          }

          2.判断特定条件,更改单元格背景颜色

          protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
          {    //数据加载时发生
                if (e.Row.RowType == DataControlRowType.DataRow)
                {    //判断是否是数据行
                      if (e.Row.Cells[8].Text == "USA")
                      {   //判断第9列值是否为"USA"
                            //e.Row.BackColor = System.Drawing.Color.Red;
                            e.Row.Cells[8].BackColor = System.Drawing.Color.Red;
                      }
                }
          }

          3.父窗体中调用子窗体的gridview

          在父窗体中调用子窗体的gridview值(简单的页面交互):

          父窗体代码:

          注:单机事件用window.open打开新窗体并获得焦点
          <head>
          <script language=javascript>...
                function openpage(htmlurl) 
                {
                      var newwin=window.open(htmlurl,"newWin","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=yes,top=100,left=200,width=650,height=300");
                      newwin.focus();
                      return false;
                }
          </script>
          </head>
          <body>
                <input type=text id="name" />
                在按钮中调用:
                <input type=button value="调用" onclick="return openpage('GridViewClientClick.aspx');" />
          </body>

          子窗体代码:

          注:girdview中e.Row.Attributes增加单击属性,ReKey并将此行第三列的值传过去
          protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
          {
                if(e.Row.RowType==DataControlRowType.DataRow)
                {
                      e.Row.Attributes.Add("ondblclick", "ReKey('" + e.Row.Cells[2].Text+"')");
                      //e.Row.Attributes["style"] = "Cursor:hand"; 
                      // //键盘事件
                      //e.Row.Attributes.Add("OnKeyDown", "GridViewItemKeyDownEvent('" + e.Row.Cells[1].Text + "')");
                }
          }

  • 相关阅读:
    HDU 2546:饭卡(01背包)
    HPU 第三次积分赛:阶乘之和(水题)
    拓扑排序练习题
    HDU 2647:Reward(拓扑排序+队列)
    HDU 3342:Legal or Not(拓扑排序)
    HDU 2094:产生冠军(拓扑排序)
    POJ 2585:Window Pains(拓扑排序)
    51Nod 1002:数塔取数问题(DP)
    cogs696 longest prefix
    poj3764 The xor-longest Path
  • 原文地址:https://www.cnblogs.com/waynewjp/p/1561424.html
Copyright © 2011-2022 走看看