2012-07-28 72 views
0

我繼承了datagridcolumnstyle,在Paint函數中,我需要根據所渲染的行中的單元格做一些事情。在「Paint」函數中獲取數據網格中某行單元格的值?

因此,例如,我有以下幾點:

row 1: color1, name, last name 
row 2: color2, name2, last name2 
row 3: color3, name3, last name3 

的名字和姓氏列需要在色列定義的顏色被塗。

所以,當有關NAME2的datagridcolumnstyle正在制訂我需要能夠做一些事情,如:

protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight) 
{ 
Color color = (Color)GetRow(rowNum).GetCell(color1).Value; 
} 
+0

我從來沒有使用[DataGridColumnStyle類](http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridcolumnstyle(v = vs.71).aspx)。鏈接的示例不會在我擁有的任何WinMoble版本下構建。你有更好的參考?它是爲了什麼? – jp2code 2012-07-30 01:24:15

回答

1

我不知道這是什麼或如何得到它的工作,但在這裏

protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, 
    Brush backBrush, Brush foreBrush, bool alignToRight) { 
    DateTime date = (DateTime)GetColumnValueAtRow(source, rowNum); 
    Rectangle rect = bounds; 
    g.FillRectangle(backBrush, rect); 
    rect.Offset(0, 2); 
    rect.Height -= 2; 
    g.DrawString(date.ToString("d"), this.DataGridTableStyle.DataGrid.Font, foreBrush, rect); 
} 

基礎上的代碼片段,我會說這個工具的原因是讀取從提供的座標的單元格的值,並更改根據顏色:從微軟的DataGridColumnStyle Class例子所示的例子那個價值。

因此,如果該值無效,可以繪製紅色單元格。

如果該值未保存,則可以將字體設置爲粗體。

但是,我發佈的示例非常適合Windows Mobile!

1

簡單的解決方案,我用過的一個方法是,在創建它時,將擁有DatGrid的引用傳遞到自定義的DataGridColumnStyle對象中。這可以通過構造函數或通過屬性完成。然後您可以在Paint期間訪問整行。

相關問題