Hide Gridview row if a column value is blank.

Posted on 2011-05-03
Last Modified: 2012-05-11
using ASP.NET in C#

Is there anyway to hide a row in Gridview if a column value is blank?

for example if my Grid view is

NAME     AGE     Country
Joe           34       USA
Jack         24  

I would like to hide Jack since we dont have his Country information. ( i would like to do this without using the WHERE statement in my SQL)

Question by:Raul77
    LVL 7

    Accepted Solution

    This page should be a good read for you...

    So you'll want to perform the action inside of the RowDataBound event... Below is a modified possibility that might more fully fit your needs than the example provided in the url above.
    void YourGridview_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            // if this is a SqlDataSource you can use DataRowView, 
            // otherwise use whatever type is used in the data source
            DataRowView rowView = (DataRowView)e.Row.DataItem;
            if (String.IsNullOrEmpty(rowView["ColumnA"])) 
                e.Row.Visible = false;

    Open in new window

    LVL 19

    Expert Comment

    by:Manoj Patil
    use the following code

    protected void grdItems_DataBound(object sender, EventArgs e)
            Boolean hasData = false;
            for (int col = 0; col < grdItems.HeaderRow.Cells.Count ; col++)
                for (int row = 0; row < grdItems.Rows.Count; row++)
                    if (!String.IsNullOrEmpty(grdItems.Rows[row].Cells[col].Text) 
                        && !String.IsNullOrEmpty(HttpUtility.HtmlDecode(grdItems.Rows[row].Cells[col].Text).Trim()))
                        hasData = true;
                     if (!hasData)
                        grdItems.HeaderRow.Cells[col].Visible = false;
                        for (int hiddenrows = 0; hiddenrows < grdItems.Rows.Count; hiddenrows++)
                            grdItems.Rows[hiddenrows].Cells[col].Visible = false;
                hasData = false;

    Open in new window

    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now