Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to display Icons in a Datagridview column? 2

Status
Not open for further replies.

HiBoo

Programmer
Jan 11, 2000
88
0
0
CA
I've researched the heck out of this one and cannot find a solution using the DataGridView control.

Using VS2005 I'm trying to display one of two Icons in a datagridview control column based on the boolean value of the columns row data.

The data source is based on a Person table with a Name and ActiveUser status field. If the ActiveUser field is true I want to display IconA, otherwise I want to display IconB. I've created a procedure to format the grid which I'm calling right after populating the datagridview control...
Code:
Private Sub FormatDataGridView()

        Dim objCellStyle As New DataGridViewCellStyle

        With objCellStyle
            .Alignment = DataGridViewContentAlignment.MiddleLeft
            .Font = New Font("Courier New", 10, FontStyle.Regular)
        End With
        Me.dgvPersons.Columns(0).DefaultCellStyle = objCellStyle
'--- Column(1) is the ActiveUser, a boolean value.
'--- I want to display IconA or IconB based on it's value.
'--- The Icons are stored in My.Resources as either icoActive or icoInactive.
        


End Sub
Can someone explain to me how I display a different Icon in the second column based on the value of the ActiveUser? In essence I want the grid to display the Persons name in the first column and the Icon in the second column.
 
This MSDN article shows how to display use a DataGridViewImageColumn to display an image. The example even uses and icon (.ico):

How to: Display Images in Cells of the Windows Forms DataGridView Control

I'm not sure how to make the column display a different image based on a particular value. I'll look into it and let you know if I find anything.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
To get another value on the same row in the CellFormatting event:
Code:
Dim dgv As DataGridView = CType(sender, DataGridView)

Dim dr As DataRow = CType(dgv.Rows(e.RowIndex).DataBoundItem, DataRowView).Row

If Convert.ToBoolean(dr("boolCol")) Then

    e.Value = icoYes

Else

    e.Value = icoNo

End If
 
Thanks jebenson and SHelton. Using code referenced from the link, I linked again to "How to: Customize Data Formatting in the Windows Forms DataGridView Control" at After modifying a bit of the code I was able to accomplish what I needed to do. I guess the guts of the work is done in the CellFormatting event as SHelton suggests. A little more tweeking and it should be great!

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top