分享

C# GridView详解

 醉人说梦 2014-08-11
  • 前台代码

Java代码 复制代码 收藏代码
  1. <!--id用于绑定数据的标识,-->   
  2. <asp:GridView ID="gvLog" runat="server" AutoGenerateColumns="False" CellPadding="3" CellSpacing="1"  
  3.     Width="100%" BackColor="#F3F3F3" BorderWidth="0px" OnRowDeleting="gvLog_RowDeleting" OnRowDataBound="gvLog_RowDataBound">   
  4.     <RowStyle BackColor="White" />   
  5.     <Columns>   
  6.      <!--绑定列-->   
  7.         <asp:BoundField DataField="OperId" >   
  8.             <ItemStyle CssClass="display:none;" />   
  9.         </asp:BoundField>   
  10.         <asp:BoundField HeaderText="No">   
  11.             <ItemStyle Width="30px" HorizontalAlign="Center" />   
  12.         </asp:BoundField>   
  13.         <!--带链接的绑定列,DataNavigateUrlFields表示要传递的参数,多个参数用逗号分割开-->   
  14.         <asp:HyperLinkField DataNavigateUrlFields="OperId,operName" DataNavigateUrlFormatString="OperatorEdit.aspx?OperId={0}&operName={1}"  
  15.             DataTextField="OperName" HeaderText="登录名称" >   
  16.             <ItemStyle Width="100px" />   
  17.         </asp:HyperLinkField>   
  18.         <asp:BoundField HeaderText="真实名称" DataField="TrueName">   
  19.             <ItemStyle Width="100px" />   
  20.         </asp:BoundField>   
  21.         <asp:BoundField HeaderText="备注" DataField="OperMemo" />   
  22.         <asp:HyperLinkField HeaderText="授权" NavigateUrl="OperatorRole.aspx" Text="角色授权" DataNavigateUrlFields="OperId" DataNavigateUrlFormatString="OperatorRole.aspx?operId={0}">   
  23.             <ItemStyle Width="60px" HorizontalAlign="Center" />   
  24.         </asp:HyperLinkField>   
  25.         <!--事件绑定操作,该例子是删除时间-->   
  26.         <asp:CommandField HeaderText="操作" ShowDeleteButton="True">   
  27.             <ItemStyle Width="40px" HorizontalAlign="Center" />   
  28.         </asp:CommandField>   
  29.     </Columns>   
  30.     <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />   
  31.     <!--表格头部样式-->   
  32.     <HeaderStyle BackColor="#CCCCCC" Font-Bold="True" ForeColor="#FFFEEE" Height="15px" />   
  33.    <!--隔行变色-->   
  34.     <AlternatingRowStyle BackColor="#F9F9F9" />   
  35. </asp:GridView>  

  • 后台代码---绑定gridview

Java代码 复制代码 收藏代码
  1. DataSet ds = operServices.GetList("");   
  2. DataView dv = new DataView(ds.Tables[0]);   
  3. ///获取或设置用于筛选在 DataView 中查看哪些行的表达式。   
  4. dv.RowFilter = "isdel=0";   
  5.   
  6. this.gvLog.DataSource = dv;   
  7. this.gvLog.DataBind();  

注1:DataView 使您能够创建 DataTable 中所存储的数据的不同视图,这种功能通常用于数据绑定应用程序。使用 DataView,您可以使用不同排序顺序显示表中的数据,并且可以按行状态或基于筛选器表达式来筛选数据。

注2:如果获取不到GridView隐藏控件的值,则需要加入以下一段代码
Java代码 复制代码 收藏代码
  1. ///该方法名称应该与你绑定的时间名称一致   
  2. protected void gvLog_RowDataBound(object sender, GridViewRowEventArgs e)   
  3. {   
  4.    if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)   
  5.     {   
  6.         ///指定隐藏列,并赋值   
  7.         e.Row.Cells[0].Visible = false;   
  8.         e.Row.Cells[1].Visible = false;   
  9.     }   
  10. }  


注3:如果我们要根据某个条件去改变表格某个值的样式,可以在行绑定时间方法中加入如下代码,如:
Java代码 复制代码 收藏代码
  1. protected void gvLog_RowDataBound(object sender, GridViewRowEventArgs e)   
  2. {   
  3.     if (e.Row.RowIndex != -1)   
  4.     {   
  5.         if (Convert.ToInt16(e.Row.Cells[1].Text.ToString()) == 1)   
  6.         {   
  7.             e.Row.Cells[6].Text = "<font color='red'><b>" + e.Row.Cells[6].Text.ToString() + "</b></font>";   
  8.         }   
  9.     }   

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多