2017-04-26 62 views
1

我想格式化關於對象的「status」屬性的datagridview的行。 FormatRow函數可以接收任何類型的對象。我想出了這個功能:函數中的泛型

Private Sub FormatRow(Of T)() 

    For Each row As DataGridViewRow In dgvHistory.Rows 

     Dim obj As T = CType(row.DataBoundItem, T) 

     If obj.**Status** = BLL.Configuration.HISTORY_STATUS_ACTIVE Then 
      row.DefaultCellStyle.ForeColor = Color.Green 
      row.DefaultCellStyle.Font = New Font(Control.DefaultFont, FontStyle.Bold) 
     End If 

    Next 

End Sub 

但我不能讓它成爲正確的方式。在IF語句中,「status」屬性不可用,因爲我的行對象沒有正確輸出。

有什麼想法?謝謝。

回答

1

我打算用C#來回答,你會進行轉換。

創建一個接口IStatus或類似包含Status-property的東西。 讓你的實體實現這個接口。

定義你的函數是這樣的:

private void FormatRow<T>() where T: IStatus 

這應該做的伎倆。

+1

它的工作原理!謝謝你的提示! – Dams