Click here to Skip to main content
11,704,509 members (55,405 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>

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 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 700
1 OriginalGriff 495
2 Andy Lanng 275
3 CPallini 242
4 Maciej Los 235
0 OriginalGriff 8,824
1 Sergey Alexandrovich Kryukov 8,247
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,606


Advertise | Privacy | Mobile
Web02 | 2.8.150819.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