2

使用 MS Visual Studio 2012、Telerik、C#.ASP.NET。

我需要的逻辑如下:

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

基本上,如果一列有 3 行数据,如果全部为空,则不要费心显示该列,但是如果其中 1 行有值,则显示该列。

一直在玩:

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

代码不起作用当然不会遍历 foreach 循环只是跳过它。尽管即使它确实迭代了也不担心,但我需要一种方法来检查所有 column[name] 行是否为空。有一个不错的 Telerik 单班轮吗?

4

2 回答 2

4

请尝试以下代码片段。

使用唯一名称

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

通过使用索引

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


    }
}
于 2013-07-03T13:15:20.737 回答
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
于 2013-07-03T12:53:58.140 回答