Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to hide nullvalues column in gridview. I got stuck to hide null values column. i hide null values column but header was also hided.
COde snnip is below:
C#
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
    {
        // Make sure there are at least header row
        if (GridView1.HeaderRow != null)
        {
            int columnIndex = 0;

            // For each column
            foreach (DataControlFieldCell clm in GridView1.HeaderRow.Cells)
            {
                bool notAvailable = true;

                // For each row
                foreach (GridViewRow row in GridView1.Rows)
                {
                    string columnData = row.Cells[columnIndex].Text;
                    if (!(string.IsNullOrEmpty(columnData) || columnData== " "))
                    {
                        notAvailable = false;
                        break;
                    }
                }

                if (notAvailable)
                {
                    // Hide the target header cell
                    GridView1.HeaderRow.Cells[columnIndex].Visible = true
                        ;

                    // Hide the target cell of each row
                    foreach (GridViewRow row in GridView1.Rows)
                        row.Cells[columnIndex].Visible = false;
                }

                columnIndex++;
            }
        }


    }
Posted
Comments
What you want to do? You want to blank out the values inside the column for each row?

1 solution

/protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
//{
// // Make sure there are at least header row
// if (GridView1.HeaderRow != null)
// {
// int columnIndex = 0;

// // For each column
// foreach (DataControlFieldCell clm in GridView1.HeaderRow.Cells)
// {
// bool notAvailable = true;

// // For each row
// foreach (GridViewRow row in GridView1.Rows)
// {
// string columnData = row.Cells[columnIndex].Text;
// if (!(string.IsNullOrEmpty(columnData) || columnData == " "))
// {
// notAvailable = false;
// GridView1.HeaderRow.Cells[columnIndex].Visible = true;
// break;
// }
// }

// if (notAvailable)
// {
// // Hide the target header cell
// GridView1.HeaderRow.Cells[columnIndex].Visible = false;
// // ;

// // Hide the target cell of each row
// foreach (GridViewRow row in GridView1.Rows)
// row.Cells[columnIndex].Visible = false;
// }

// columnIndex++;
// }
// }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900