Question

Using MS Visual Studio 2012, Telerik, C#.ASP.NET.

The logic I need is as follows:

If a columns data on all rows is null then hide the column

Basically if a column has 3 rows of data, if there all null then dont bother displaying that column, however if there is a value in 1 of them, then show the column.

been playing around:

foreach (GridColumn columns in dgvUserResults.Columns)
{
    if (columns != null)
    {
        columns.Visible = false;
    }
    else
    {
        columns.Visible = true;
    }
}

code doesn't work of course doesnt iterate through the foreach loop just skips it. Although not bothered about that even if it did iterate through I need a way to check if all column[name] rows are null. There a nice Telerik one liner?

Was it helpful?

Solution

Please try with below code snippet.

Using UniqueName

protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    foreach (GridColumn column in RadGrid1.MasterTableView.Columns)
    {
        // If you used ClientSelectColumn then below code is not worked For that you have to put condition
        //if(column.ColumnType  == "GridBoundColumn")

        int TotalNullRecords = (from item in RadGrid1.MasterTableView.Items.Cast<GridDataItem>()
                                where string.IsNullOrWhiteSpace(item[column.UniqueName].Text) ||
                                item[column.UniqueName].Text == "&nbsp;"
                                select item).ToList().Count;

        if (TotalNullRecords == RadGrid1.MasterTableView.Items.Count)
        {
            RadGrid1.MasterTableView.Columns.FindByUniqueName(column.UniqueName).Visible = false;
        }
    }
}

By Using Index

protected void RadGrid1_PreRender(object sender, EventArgs e)
{


    foreach (GridColumn column in RadGrid1.MasterTableView.Columns)
    {
        // If you used ClientSelectColumn then below code is not worked For that you have to put condition
        //if(column.ColumnType  == "GridBoundColumn")

        int TotalNullRecords = (from item in RadGrid1.MasterTableView.Items.Cast<GridDataItem>()
                                where string.IsNullOrWhiteSpace(item[column.UniqueName].Text) ||
                                item[column.UniqueName].Text == "&nbsp;"
                                select item).ToList().Count;

        if (TotalNullRecords == RadGrid1.MasterTableView.Items.Count)
        {
            column.Visible = false;
        }


    }
}

OTHER TIPS

  For col = 0 To myRadGridView.ColumnCount
        Dim mustKeepColumn As Boolean = False
        For Each r In myRadGridView.Rows

            If Not String.IsNullOrEmpty(r.Cells(col).Value.ToString) Then
                mustKeepColumn = True
                Exit For
            End If

        Next
        If Not mustKeepColumn Then
            myRadGridView.Columns(col).IsVisible = False
        End If
    Next
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top