国产探花免费观看_亚洲丰满少妇自慰呻吟_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>


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 香港 | 上虞市| 镇远县| 南华县| 南宫市| 新绛县| 宣武区| 南华县| 密云县| 金门县| 措美县| 灵川县| 华宁县| 韩城市| 阿瓦提县| 西乡县| 乐都县| 永德县| 扎囊县| 木兰县| 晋城| 天祝| 临武县| 阿拉善左旗| 巴里| 隆安县| 大连市| 如皋市| 洪泽县| 福州市| 辉县市| 咸宁市| 黄石市| 泽库县| 渝北区| 虎林市| 通城县| 陆丰市| 清苑县| 海门市| 松原市|