469,914 Members | 2,363 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,914 developers. It's quick & easy.

Datagridview: different images for different rows

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,

Mar 26 '08 #1
4 11145
On Wed, 26 Mar 2008 13:41:41 -0300, "CGatto"
<cg****@pleaseremoveme.gnb.cawrote:
>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.

Mar 26 '08 #2
Check out the CellFormatting event on the DataGridView.

CellFormatting happens between the data source and the screen.

Add an event handler:

AddHandler myGrid.CellFormatting, AddressOf OnCellFormatting

Then code it:

Private Sub OnCellFormatting(ByVal object as Sender, _
ByVal e as DataGridViewCellFormattingEventArgs)

If e.ColumnIndex = myGrid.Columns("columnIcareAbout") 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

'now you can check the value of your other column and paint
' whatever you want in this column
'get the row being populated; format this cell
End If
End Sub

If that doesn't get you far enough, post back. There is an example in Brian
Noyes' Data Binding book of this. It's just too late for me to go track it
down right now.

RobinS.
"CGatto" <cg****@pleaseremoveme.gnb.cawrote in message
news:eV**************@TK2MSFTNGP03.phx.gbl...
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,
Mar 27 '08 #3
"CGatto" <cg****@pleaseremoveme.gnb.cawrote in message
news:eV**************@TK2MSFTNGP03.phx.gbl...
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.
Mar 27 '08 #4

"CGatto" <cg****@pleaseremoveme.gnb.cawrote in message
news:ee**************@TK2MSFTNGP03.phx.gbl...
"CGatto" <cg****@pleaseremoveme.gnb.cawrote in message
news:eV**************@TK2MSFTNGP03.phx.gbl...
>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.

Congratulations! And thanks for posting the answer back.

RobinS.

Mar 28 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Spike | last post: by
1 post views Thread by RugbyTravis | last post: by
2 posts views Thread by Jurjen de Groot | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.