2011-07-07 24 views

回答

2

到GridView

添加OnRowDataBound事件處理程序在事件處理程序 - 檢查頭部和處理每列相應

protected virtual void OnRowDataBound(GridViewRowEventArgs e) { 
    if (e.Row.RowType == DataControlRowType.Header) { 
     // process your header here.. 
    } 
} 

欲瞭解更多信息請點擊這裏:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewroweventargs.row.aspx

+1

將圖像添加到headertemplate並在onrowdatabound事件中隱藏它或將其分配給一個url。 – anami

0

我是不完全確定如何添加圖像......但this link有一個在行標題中編寫數字的好例子,因此您可以使用> 10條件更新它以顯示圖像。 。

+0

我不想號碼添加到行標題。我想在不同的場合添加不同的圖像添加行標題。請檢查閱讀的單元格內容。您可以幫助我...... – hmlasnk

+0

哦對不起,我錯過了單元格值檢查,我不確定是否可以做到這一點通過這個事件。抱歉! – nbz

2

你可以將圖像添加到DataGridView.RowPostPaint事件中的DataGridView行標題中。

下面是在CodeProject上的文章,似乎描述的這個相當不錯的鏈接(我還沒有嘗試過對自己代碼):Row Header Cell Images in DataGridView

可以使用RowPostPaint事件提取要測試對值確定要顯示哪個圖標。通過使用該事件的RowIndex屬性與你感興趣的列的索引值做到這一點

像這樣的東西應該作爲一個起點:

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { 
    // As an example we'll check contents of column index 1 in our DGV. 
    string numberString = dataGridView1.Rows[e.RowIndex].Cells[1].Value as string; 
    if (numberString != null) { 
     int number; 
     if (Int32.TryParse(numberString, out number)) { 
      if (number > 10) { 
       // Display one icon. 
      } else { 
       // Display the other icon. 
      } 
     } else { 
      // Do something because the string that is in the cell cannot be converted to an int. 
     } 
    } else { 
     // Do something because the cell Value cannot be converted to a string. 
    } 
} 
+0

Thiks很多。這對我的程序有用..... thnx再次... – hmlasnk

相關問題