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...........
3 Answers
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

- 152
- 1
- 7
-
1Add the image to the headertemplate and in the onrowdatabound event hide it or assign it with a url. – anami Jul 07 '11 at 16:41
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.
}
}

- 53,046
- 9
- 139
- 151
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. .

- 3,806
- 3
- 28
- 56
-
I don't want to add number to row header. I want to add different image add row header in different occasion.it check on read cell content.can you help me...... – hmlasnk Jul 07 '11 at 16:40
-
oh sorry I missed the cell value check, I'm not sure if you can do that through this event. Sorry! – nbz Jul 07 '11 at 16:49