如何从gridview中删除运行时不需要的列? [英] How to remove unwanted column at runtime from gridview?

查看:63
本文介绍了如何从gridview中删除运行时不需要的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过使用datatable来绑定gridview。我希望在特定条件下删除gridview列。即如果任何列具有null值,那么将省略显示的特定列...



以下代码我写在我的.aspx页面上

I am binding gridview by using datatable..i want to remove gridview column on specific condition.that is if any column have null value then that perticular column will be omitted from being displayed..

following code i have written on my .aspx page

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" 
         Height="50px" EmptyDataText="No Records Found" 
                             
         Width="885px">
         
         <Columns>
         
         <asp:TemplateField  >
                            <ItemTemplate>
                                <div class="gridcolstyle">
                                    <asp:Label ID="lblfid" runat="server" CssClass="labelstyle" Text='<%# bind("Sr") %>'></asp:Label>
                                </div>
                            </ItemTemplate>
                            
                        </asp:TemplateField>
                        
         <asp:TemplateField  >
                            <ItemTemplate>
                                <div class="gridcolstyle">
                                    <asp:Label ID="lblfid" runat="server" CssClass="labelstyle" Text='<%# bind("Staffname") %>'></asp:Label>
                                </div>
                            </ItemTemplate>

                        </asp:TemplateField>
          <asp:TemplateField >
                            <ItemTemplate>
                                <div class="gridcolstyle">
                                    <asp:Label ID="lblfid" runat="server" CssClass="labelstyle" Text='<%# bind("Exps1") %>'></asp:Label>
                                </div>
                            </ItemTemplate>
                           
                        </asp:TemplateField>
           <asp:TemplateField  >
                            <ItemTemplate>
                                <div class="gridcolstyle">
                                    <asp:Label ID="lblfid" runat="server" CssClass="labelstyle" Text='<%# bind("Exps2") %>'></asp:Label>
                                </div>
                            </ItemTemplate>
                            
                        </asp:TemplateField>
           <asp:TemplateField  >
                            <ItemTemplate>
                                <div class="gridcolstyle">
                                    <asp:Label ID="lblfid" runat="server" CssClass="labelstyle" Text='<%# bind("Exps2") %>'></asp:Label>
                                </div>
                            </ItemTemplate>
                            
                        </asp:TemplateField>
           <asp:TemplateField  >
                            <ItemTemplate>
                                <div class="gridcolstyle">
                                    <asp:Label ID="lblfid" runat="server" CssClass="labelstyle" Text='<%# bind("Exps2") %>'></asp:Label>
                                </div>
                            </ItemTemplate>
                            
                        </asp:TemplateField>

         <asp:TemplateField  >
                            <ItemTemplate>
                                <div class="gridcolstyle">
                                    <asp:Label ID="lblfid" runat="server" CssClass="labelstyle" Text='<%# bind("Exps3") %>'></asp:Label>
                                </div>
                            </ItemTemplate>
                            
                        </asp:TemplateField>
            <asp:TemplateField  >
                            <ItemTemplate>
                                <div class="gridcolstyle">
                                    <asp:Label ID="lblfid" runat="server" CssClass="labelstyle" Text='<%# bind("Exps4") %>'></asp:Label>
                                </div>
                            </ItemTemplate>
                            
                        </asp:TemplateField>







string str1 = "select * from ExpenseTrans";
           DataTable dt1 = db.GetDataTable(str1);
      
           if (dt1.Rows.Count > 0)
           {
               GridView1.DataSource = dt1;
               GridView1.DataBind();
}

推荐答案

在ASP.NET中显示/隐藏GridView列 [ ^ ]应该可以帮到你。



查看这些也是 -

http:// dotnetspeaks .net / post / Hiding-Columns-in-GridView-without-lose-data.aspx [ ^ ]

http://csharpdotnetfreak.blogspot.in/2008/12/hide-gridview-columns-in-normal-mode.html [<一个HREF =http://csharpdotnetfreak.blogspot.in/2008/12/hide-gridview-columns-in-n ormal-mode.htmltarget =_ blanktitle =新窗口> ^ ]
Show / Hide GridView Columns in ASP.NET[^] should help you out.

Check out these as well -
http://dotnetspeaks.net/post/Hiding-Columns-in-GridView-without-losing-data.aspx[^]
http://csharpdotnetfreak.blogspot.in/2008/12/hide-gridview-columns-in-normal-mode.html[^]


这篇关于如何从gridview中删除运行时不需要的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆