Solved

How determine if gridview column has no data?

Posted on 2010-09-20
3
725 Views
Last Modified: 2013-12-16
I have a gridview that I bind with code. How can I determine if a gridview column has no data? I want to hide that column if it doesn't have data.
0
Comment
Question by:RobertNZana
3 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 125 total points
ID: 33723188
you can simply iterate throw all rows in the datagrid and check if the cell in requested column is empty.
something like:
            DataGridView dgv = new DataGridView();
            int count = dgv.Rows.Cast<DataGridViewRow>().Where(n => n.Cells[columnName].Value != null && !string.IsNullOrEmpty(n.Cells[columnName].Value.ToString())).Count();
            if (count == 0)
            {
                //column is empty and has no data
            }
0
 
LVL 22

Assisted Solution

by:Mrunal
Mrunal earned 125 total points
ID: 33723790
you can also do like this:

protected void MyGrid_RowCreated(object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.Header)
 {
  //check here for black or null data. If found...then execute...
  e.Row.Cells[index].Visible = false;
 }
 if(e.Row.RowType == DataControlRowType.DataRow)
 {
  //check here for black or null data. If found...then execute...
  e.Row.Cells[index].Visible = false;
 }
}
0
 
LVL 9

Author Closing Comment

by:RobertNZana
ID: 33756200
will take a look later
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

766 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