2013-07-03 126 views
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循環只是跳過它當然犯規迭代的工作。雖然不打擾,即使它迭代通過我需要一種方法來檢查是否所有列[名稱]行是空的。有一個很好的Telerik one liner?

+0

你只是他們不爲空(列本身是存在的)。對於每一列,您需要遍歷每行,並檢查所有行中該列的所有單元格的所有值是否都爲空。 – Corak

+0

idd thats correct像我sed代碼wudnt工作,不知道如何迭代通過行和隱藏時,都是null任何示例代碼? – lemunk

+0

「不遍歷foreach循環只是跳過它」。它只會在'dgvUserResults.Columns'沒有項目的時候這樣做。你確定'dgvUserResults'在那個時候被正確初始化了嗎? – Corak

回答

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


    } 
} 
+0

不錯的答案只是得到一個小的語法錯誤,未知的方法「在哪裏(?)」system.collections.generic.ITnumerable lemunk

+0

如果可能,那麼請你提供完整的錯誤細節。 –

+0

嗯,我只是運行代碼來獲取錯誤,它給我試圖構建的唯一錯誤是:錯誤'字符串'不包含'IsNullOrWhiteSpace'的定義。 IsNullOrEmpty會工作嗎? – lemunk

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 
+0

這將與telerik RadGridView – dimis164

+0

一起工作現在有radcrid上的columnCount當我說dgvuserResults – lemunk

+0

它的一個telerik radgrid控件 – lemunk

相關問題