簡體   English   中英

如何在 c# 中的 datagridview 行 header 中添加不同的圖標/圖像?

[英]How to add different icons/images to datagridview row header in c#?

我想動態地將不同的圖像添加到 c# windows 表單 datagridview 行 header 。 它應該像檢查任何單元格值一樣,如果它>10 顯示一些圖像,否則顯示其他圖像。如何做到這一點?請幫助我............

將 OnRowDataBound 事件處理程序添加到 GridView

在事件處理程序中 - 檢查 header 並相應地處理每一列

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

有關更多信息 go 此處: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridrowview。

您可以在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.
    }
}

我不完全確定如何添加圖像......但這個鏈接有一個很好的例子,在行標題中寫入數字,所以你可以用 >10 條件更新它,以顯示你的圖像。 .

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM