国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

DPC:Hiding Columns In A DataGrid[等級:初 中]

2024-07-21 02:16:49
字體:
來源:轉載
供稿:網友
hiding columns in a datagrid

one of the frequently asked questions over at asplists.com is: "how do i hide a column in a datagrid?". one very important point to note is that you cannot hide autogenerated columns in a data grid. the reason for this is that autogenerated columns are not added to the datagrid's datagridcolumn collection. so, in order for a column to be hidden it must be either programmatically added to the datagrid at runtime or explicitly defined (using templates) at design time with the autogeneratecolumns property set to false (the code in this article will use this method).

this article provides sample code to hide a column (could easily be modified to hide more) for two different scenarios - hiding columns in response to an event on the page (common in web reports) or hiding columns to provide different functionality based on security levels.

first, let's look at how we can hide and show a column in a datagrid in response to an event (the click of a button) on the page. you can see a live example here.

the code:

<%@ page language="vb" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.sqlclient" %>
<html>
<script runat="server">
sub page_load(sender as object, e as eventargs)
    dim myconnection as sqlconnection = new _
            sqlconnection("data source=(local)/netsdk; trusted_connection=yes; initial catalog=pubs")

    dim mycommand as sqlcommand = new sqlcommand("select * from publishers", myconnection)
    
    myconnection.open()

    mydatagrid.datasource = mycommand.executereader(commandbehavior.closeconnection)
        mydatagrid.databind()
end sub

sub hideshow_click(sender as object, e as eventargs)
    if mydatagrid.columns(0).visible = false then
        mydatagrid.columns(0).visible = true
    else
        mydatagrid.columns(0).visible = false
    end if
end sub
</script>
<body>
<form runat="server">
<asp:datagrid id="mydatagrid" width="25%" autogeneratecolumns="false" runat="server">
<columns>
<asp:templatecolumn headertext="publisher's id">
  <itemtemplate>
      <span><%# container.dataitem("pub_id") %></span>
  </itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="publisher's name">
  <itemtemplate>
      <span><%# container.dataitem("pub_name") %></span>
  </itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="city">
  <itemtemplate>
      <span><%# container.dataitem("city") %></span>
  </itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="state">
  <itemtemplate>
      <span><%# container.dataitem("state") %></span>
  </itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="country">
  <itemtemplate>
      <span><%# container.dataitem("country") %></span>
  </itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
<asp:button id="hideshow" text="hide/show" onclick="hideshow_click" runat="server" />
</form>
</body>
</html>

next, let's look at how we can hide a column in a datagrid based on the security level of the user. this example is not *actually* secure and security is not discussed in this article - we are trying to show how to hide datagrid columns not how to secure a page! the code below checks a querystring parameter called 'security' each time the page loads. if 'security' equals "admin" then all of the columns are displayed. if 'security' equals anything other than "admin" then the first column in the datagrid is hidden.

click here to view the datagrid with no priveleges. (no pub_id column)
click here to view the datagrid with admin priveleges. (all columns visible)

the code:

<%@ page language="vb"%>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.sqlclient" %>
<html>
<script runat="server">
sub page_load(sender as object, e as eventargs)
    dim myconnection as sqlconnection = new _
            sqlconnection("data source=(local)/netsdk; trusted_connection=yes; initial catalog=pubs")

    dim mycommand as sqlcommand = new sqlcommand("select * from publishers", myconnection)
    
    myconnection.open()

    mydatagrid.datasource = mycommand.executereader(commandbehavior.closeconnection)
        mydatagrid.databind()

    if request.querystring("security") = "admin" then
        mydatagrid.columns(0).visible = false
    end if    
end sub
</script>
<body>
<form runat="server">
<asp:datagrid id="mydatagrid" autogeneratecolumns="false" runat="server">
<columns>
<asp:templatecolumn headertext="publisher's id">
  <itemtemplate>
      <span><%# container.dataitem("pub_id") %></span>
  </itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="publisher's name">
  <itemtemplate>
      <span><%# container.dataitem("pub_name") %></span>
  </itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="city">
  <itemtemplate>
      <span><%# container.dataitem("city") %></span>
  </itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="state">
  <itemtemplate>
      <span><%# container.dataitem("state") %></span>
  </itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="country">
  <itemtemplate>
      <span><%# container.dataitem("country") %></span>
  </itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
</form>
</body>
</html>


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 什邡市| 惠州市| 闽侯县| 潞城市| 安丘市| 平顺县| 成武县| 秀山| 大丰市| 海盐县| 乐亭县| 南开区| 元江| 通州区| 海晏县| 册亨县| 壤塘县| 青州市| 临沧市| 武陟县| 彰武县| 绥江县| 佳木斯市| 鄂温| 荥阳市| 泰来县| 淅川县| 固原市| 鄂尔多斯市| 安福县| 河源市| 安仁县| 上杭县| 监利县| 南京市| 佛坪县| 汤原县| 上思县| 安陆市| 尉氏县| 安徽省|