开发者

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

开发者 https://www.devze.com 2023-03-18 13:29 出处:网络
I want to add different image to c# windows form datagridview row header dynamically. it should be do like check any cell value and if it>10 display some image,else display other image.How开发者_运维问

I want to add different image to c# windows form datagridview row header dynamically. it should be do like check any cell value and if it>10 display some image,else display other image.How开发者_运维问答 to do this one?please help me...........


Add a OnRowDataBound event handler to the GridView

In the event handler - check for the header and process each column accordingly

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

For more info go here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewroweventargs.row.aspx


You can add images to DataGridView row headers in the DataGridView.RowPostPaint event.

Here's a link to an article on CodeProject that appears to describe this fairly well (I haven't tried to code myself): Row Header Cell Images in DataGridView

You can use the RowPostPaint event to extract the value you want to test against to determine which icon you want to display. Do this by using the event's RowIndex property with the index value of the column you're interested in.

Something like this should serve as a starting point:

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.
    }
}


I'm not exactly sure how to add images... but this link has a good example of writing numbers in the row headers, so you could update it with the >10 condition, to display your image. .

0

精彩评论

暂无评论...
验证码 换一张
取 消