Click here to Skip to main content
15,886,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
<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>


C#
string str1 = "select * from ExpenseTrans";
           DataTable dt1 = db.GetDataTable(str1);
      
           if (dt1.Rows.Count > 0)
           {
               GridView1.DataSource = dt1;
               GridView1.DataBind();
}
Posted
Comments
oliver grace 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900