Click here to Skip to main content
12,508,491 members (56,014 online)
Rate this:
 
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>

string str1 = "select * from ExpenseTrans";
           DataTable dt1 = db.GetDataTable(str1);
      
           if (dt1.Rows.Count > 0)
           {
               GridView1.DataSource = dt1;
               GridView1.DataBind();
}
Posted 1-Feb-13 19:08pm
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160927.1 | Last Updated 2 Feb 2013
Copyright © CodeProject, 1999-2016
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