2

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

私が必要とするロジックは次のとおりです。

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

基本的に、列に3行のデータがある場合、すべてnullの場合はその列を表示する必要はありませんが、そのうちの1つに値がある場合は列を表示します。

遊んでいました:

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

コードはもちろん機能しません foreach ループを反復処理するだけでスキップします。たとえそれが繰り返されたとしても気にしませんが、すべての column[name] 行が null であるかどうかを確認する方法が必要です。素敵な Telerik ワンライナーはありますか?

4

2 に答える 2

4

以下のコード スニペットで試してください。

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

インデックスを使用して

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 に答える