2

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?

Jayesh Goyani
  • 11,008
  • 11
  • 30
  • 50
lemunk
  • 2,616
  • 12
  • 57
  • 87
  • You're just going through all the columns and of course they're not null (the column itself is there). For each column you need to go through each row and check if all the values of all the cells for that column in all rows are null. – Corak Jul 03 '13 at 12:07
  • idd thats correct like i sed that code wudnt work, not sure how to iterate through the rows and hide when all are null any example code? – lemunk Jul 03 '13 at 12:22
  • "doesnt iterate through the foreach loop just skips it". It would only do that if `dgvUserResults.Columns` has no items. Are you sure `dgvUserResults` is initialized correctly at that point? – Corak Jul 03 '13 at 12:34
  • the dgv datasource is filled before this point, i think the dgv is also rebinded – lemunk Jul 03 '13 at 12:43

2 Answers2

4

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;
        }


    }
}
Jayesh Goyani
  • 11,008
  • 11
  • 30
  • 50
  • nice answer just getting a little syntax error, unknow method "where(?)" system.collections.generic.ITnumerable – lemunk Jul 03 '13 at 13:45
  • if possible then can you please provide full detail of error. – Jayesh Goyani Jul 03 '13 at 13:51
  • well i just ran the code to get the error, the only error its giving me on attempted build is : Error 'string' does not contain a definition for 'IsNullOrWhiteSpace'. would IsNullOrEmpty work? – lemunk Jul 03 '13 at 13:55
  • Then please try "string.IsNullOrEmpty" Because "string.IsNullOrWhiteSpace" is available from .net framework 4.0 onwards. – Jayesh Goyani Jul 03 '13 at 13:57
  • isnullorempty fixed the syntax error. but i have a bug, as i debug and step through, as it attempts to run the foreach loop it breaks out and doesnt run...any ideas as to why? – lemunk Jul 03 '13 at 13:58
  • Please try with "RadGrid1.MasterTableView.AutoGeneratedColumns". – Jayesh Goyani Jul 03 '13 at 14:00
  • damn was stepping through the loop after your autogen fix and got a error, when it hit a column that was null in all the rows it errored in the code in the IF statement, error as follows: Telerik.Web.UI.GridException: Cannot find column with UniqueName 'ModuleNum9' – lemunk Jul 03 '13 at 14:03
  • i suspect its to do with the autogen fix? – lemunk Jul 03 '13 at 14:04
  • ah nevermidn i simply called column.visible from the loop...thanks for the solution – lemunk Jul 03 '13 at 14:10
0
  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
dimis164
  • 130
  • 9