Click here to Skip to main content
11,493,154 members (57,442 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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();
}
Posted 1-Feb-13 20:08pm
Comments
oliver grace at 2-Feb-13 1:14am
   
Try this,
dt1.columns.contains("null")
otherwise try to call this in
dt1=dt1.columns.remove(dt1.columns.contains("null"));
if (dt1.Rows.Count > 0)
{
GridView1.DataSource = dt1;
GridView1.DataBind();

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 419
1 Maciej Los 286
2 F-ES Sitecore 224
3 KarstenK 210
4 CPallini 208
0 Sergey Alexandrovich Kryukov 10,132
1 OriginalGriff 8,630
2 Sascha Lefèvre 3,624
3 Maciej Los 3,392
4 Richard Deeming 2,500


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 2 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100