?
Solved

Hide Gridview row if a column value is blank.

Posted on 2011-05-03
4
Medium Priority
?
2,874 Views
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)

Thanks,
0
Comment
Question by:Raul77
3 Comments
 
LVL 7

Accepted Solution

by:
JosephEricDavis earned 2000 total points
ID: 35514945
This page should be a good read for you...
http://stackoverflow.com/questions/5195964/how-can-i-hide-empty-columns-in-a-gridview-without-knowing-which-will-be-empty

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

0
 
LVL 19

Expert Comment

by:Manoj Patil
ID: 35515971
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;

                    break;

                }

            }


                 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

0
 
LVL 143

Expert Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Screencast - Getting to Know the Pipeline
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

839 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