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

首頁 > 開發 > 綜合 > 正文

如何在DataGrid控件中隱藏列

2024-07-21 02:16:48
字體:
來源:轉載
供稿:網友
如何在datagrid控件中隱藏列
前言:
    asp.net在提供我們豐富的內置控件方便我們開發的同時,一些格式化的東西也限制了我們靈活的要求。在datagrid控件中,我們遇到的一個非常典型的問題就是如何把我們不想顯示的列根據需要隨時隱藏掉。
解決方案:
    我們不能隱藏在datagrid里自動生成列的主要的一點原因是:datagrid里的column不能被datagrid的屬性datagridcolumn增加。
    基于以上原因,我們可以提出兩種不同的方案來解決這個問題。
    方案一:
    根據頁面請求的事件來隱藏列:
代碼:
<%@ 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>
程序執行執行演示:
show:

                                (圖show)
hide

                               (圖hide)
   方案二:
   
   方案二實際上是一的變通,我就簡單的介紹一下。在一中我們通過button的onclick事件來判斷是show還是hide,那么我們也可以通過聯接獲取參數的值來做判斷。
代碼:
<%@ 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>
程序執行演示:
1.security參數的值:nothing

                                  (圖:hidde)
security參數:admin

                                  (圖:show)   
                             
翻譯:cnet   轉自:http://www.aspalliance.com/    全文完
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 会昌县| 广元市| 闽清县| 渭南市| 楚雄市| 东港市| 来宾市| 抚宁县| 开封县| 尚义县| 宝应县| 治县。| 朝阳县| 仪征市| 镇江市| 英吉沙县| 林甸县| 兴化市| 桑日县| 阿拉尔市| 丘北县| 高密市| 和政县| 通河县| 万宁市| 临猗县| 普格县| 连山| 磐安县| 城步| 桦甸市| 漳州市| 湾仔区| 和田县| 比如县| 惠来县| 庄河市| 东莞市| 绥江县| 六安市| 烟台市|