Datagridview: different images for different rows

C

CGatto

Hello,

Just wondering if anyone has ever managed to find a way to have a
datagridviewimagecolumn display different images on different rows depending
on some data element in the row. Our application needs to display a series
of fees charged against an account. Some of those fees are charged at a full
rate, others at a partial rate, and some are not charged at all. The user
would like to see a simple 3-state graphic which would convey the simple
info: full-charge, partial-charge, or no-charge. Sounds easy enough but
I've not yet managed to find a way to do this. Every time I attempt to
modify the individual cell's graphic it seems to want to change the graphic
for all the rows.

Any way around this issue?

Thanks,
 
J

Jack Jackson

Hello,

Just wondering if anyone has ever managed to find a way to have a
datagridviewimagecolumn display different images on different rows depending
on some data element in the row. Our application needs to display a series
of fees charged against an account. Some of those fees are charged at a full
rate, others at a partial rate, and some are not charged at all. The user
would like to see a simple 3-state graphic which would convey the simple
info: full-charge, partial-charge, or no-charge. Sounds easy enough but
I've not yet managed to find a way to do this. Every time I attempt to
modify the individual cell's graphic it seems to want to change the graphic
for all the rows.

Any way around this issue?

Thanks,

The last entry here
<http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=200583&SiteID=1>
might be what you want.
 
C

CGatto

CGatto said:
Hello,

Just wondering if anyone has ever managed to find a way to have a
datagridviewimagecolumn display different images on different rows
depending on some data element in the row. Our application needs to
display a series of fees charged against an account. Some of those fees
are charged at a full rate, others at a partial rate, and some are not
charged at all. The user would like to see a simple 3-state graphic which
would convey the simple info: full-charge, partial-charge, or no-charge.
Sounds easy enough but I've not yet managed to find a way to do this.
Every time I attempt to modify the individual cell's graphic it seems to
want to change the graphic for all the rows.

Any way around this issue?

Thanks,
Thank you Jack and RobinS!

Both of your anwers pointed me in the right direction and I have my
solution - a huge relief! Robin's post is probably the closest to my final
code which looks like this:

Sub gvActivities_CellFormatting(ByVal sender As Object, ByVal e As
DataGridViewCellFormattingEventArgs) Handles gvActivities.CellFormatting
If e.RowIndex < 0 Then Exit Sub

If gvActivities.Columns(e.ColumnIndex).Name = "fee_type" Then

'setting this to true signals the grid that this column is being
dynamically updated. If you don't
'do this, you will get an infinite loop if you have also set the
column to have its size automatically
'determined.
e.FormattingApplied = True

Select Case
gvActivities.Rows(e.RowIndex).Cells("code_cfl_id").Value
Case 1001
e.Value = My.Resources.FullChargeIcon
Case 1002
e.Value = My.Resources.NoChargeIcon
Case Else
e.Value = My.Resources.PartialChargeIcon
End Select
End If
End Sub

I had actutally had that code in place prior to my post, however it was
missing the "e.FormattingApplied = True" line which has proven to be the
secret ingrediant. If you leave it out the "e.Value = image" wants to
change all the other images on each row along with the one you are changing.

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top