2011-12-01 97 views
0

我有一個Gridview,用於顯示客戶付款數據。默認情況下,我使用一些只能在RowDataBound事件中輕鬆獲得的檢查來更改包含過期客戶的所有行的顯示。我想添加選項來過濾掉數據,只顯示基於輸入的行或未行的行。做這個的最好方式是什麼?如何在RowDataBound事件中有條件地綁定GridView行?

我的線沿線的一些思考:

protected void gvTenantList_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (null != e.Row.DataItem) 
    { 
     DataRowView rowView = (DataRowView)e.Row.DataItem; 
     if (hsPastDueLeases.Contains((int)rowView["LeaseID"])) 
     { 
      e.Row.CssClass += " pinkbg"; 
      if (showCurrentOnly) //code to prevent showing this row 
     } 
     else if (showPastDueOnly) //code to prevent showing this row 
    } 
} 

基本上,我需要知道在//code to prevent showing this row

回答

1

爲什麼你做綁定之前沒有你做的過濾屬於什麼?

例如

gvTenantList.DataSource = data.Where(a=> !hsPastDueLeases.Contains(a.LeaseID)); // Of course you have a datatable so this is not 100% as easy as this 

或者你也可以設置行使用

e.Row.Visible = false; 


protected void gvTenantList_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (null != e.Row.DataItem) 
    { 
     DataRowView rowView = (DataRowView)e.Row.DataItem; 
     if (hsPastDueLeases.Contains((int)rowView["LeaseID"])) 
     { 
      e.Row.CssClass += " pinkbg"; 
      e.Row.Visible = !showCurrentOnly; 
     } 
     else if (showPastDueOnly){ //code to prevent showing this row 
      e.Row.Visible = false; 
     } 
    } 
} 

另外,您可以添加一個名爲的CssClass是無形的「隱藏」和CSS有

.hidden { display: none; } 

但大部分我認爲你只應該綁定你真正想要的東西,並把綁定事件中的業務邏輯放在這裏。

+0

我對這種類型的數據功能並不是很熟悉,但乍一看,它看起來好像可以工作,並且比我想象的要乾淨得多。我只是有一個條件來控制它使用哪個數據源分配,基於過濾器控制。我喜歡;會給它一個鏡頭! –

+0

爲了公平起見,使用中間的一個 - e.Row.Visible。第一個更好,但如果你不得不在一段時間內使用它,那麼稍後再做。最後一個不太理想,因爲它仍然被髮送給客戶。 –

+0

我得到一個運行時異常:[NotSupportedException:方法'布爾包含(Int32)'沒有支持到SQL的轉換。] –

相關問題