Image in DataGridView
I want to use images/pictures/icons (coming from the ressources) in aDataGridView in a separate field. Thepicture shown in this field will depend on a value of another field.
Can anybody help me?
I want to use images/pictures/icons (coming from the ressources) in aDataGridView in a separate field. Thepicture shown in this field will depend on a value of another field.
Can anybody help me?
If it's databound, I have no clue (that's not true - I have a clue!), but if it's not databound you can simply set the column to an Image column and set the image (value) as required.
If it's DataBound I suspect you may have to add the column at runtime then populate the value as each row is added - I'm not sure about that last part, but a bit of playing around with a DataGridView may prove fruitful.
In fact, it's not databound. I've added a column (DataGridViewImageColumn) and tried adding the picture to this field (when a certain condition was met in another field) in the function "CellFormatting" of the DataGrid, but no luck.
I tried to adapt the following code: http://msdn2.microsoft.com/en-us/library/z1cc356h.aspx
Did you try simply setting the image value when you set the other column?
The DataGridView has a lot of events, and can get confusing (the link you posted should do what you need), but sometimes simplicity is best: create a routine which updates the row. In that routine, you can set the image in the image column at the same time. Having said all that, what precisely isn't working? Perhaps show a small amount of code replicating your issue.
So I think I need to take a column out of this datasource and switch it to DataGridViewImageColumn, and then progress as in the example of MSDN. But how do I do that?
Here's the code
<data of the datagrid is coming from an unbound datasource>
With DataGrid1
.AutoGenerateColumns = True
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
.RowHeadersVisible = True
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.Columns.AddRange(New DataGridViewImageColumn)
.Columns(0).Name = ("Priority")
End With
Private Sub DataGrid1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGrid1.CellFormatting
If DataGrid1.Columns(0).Name.Equals("Priority") Then
Dim str_Value As String = TryCast(e.Value, String)
If str_Value Is Nothing Then Return
Dim cell As DataGridViewCell = DataGrid1(e.ColumnIndex, e.RowIndex)
cell.ToolTipText = str_Value
Select Case str_Value
Case "Condition1"
DataGrid1(e.ColumnIndex, e.RowIndex).Value = "some path....\danger.png"
Case "Condition2"
DataGrid1(e.ColumnIndex, e.RowIndex).Value = "some path....\cancel.png"
End Select
End If
End Sub
Seppe001,
According to your problem, I suggest you to use the DataGridViewImageColumn.Image property that gets or sets the image displayed in the cells of this column when the cell's Value property is not set and the cell's ValueIsIcon property is set to false.
The Image property specifies an image that is displayed in cells with no values when the column is not data-bound and the cell's ValueIsIcon property is set to false. For a data-bound column whose cells do not have an associated image, a standard error graphic is displayed.
If you want to display an Icon instead of an Image, set the Icon property instead and set the ValuesAreIcons property to true. This ensures that the alpha channel of the Icon is painted correctly. You can also set the ValueIsIcon property of individual cells to indicate whether the cell displays the Image or the Icon property value when there is no cell value.
The following code example demonstrates how to set the default image.
Private Sub CreateColumns()
Dim imageColumn As DataGridViewImageColumn
Dim columnCount As Integer = 0
Do
Dim unMarked As Bitmap = blank
imageColumn = New DataGridViewImageColumn()
' Add twice the padding for the left and
' right sides of the cell.
imageColumn.Width = x.Width + 2 * bitmapPadding + 1
imageColumn.Image = unMarked
imageColumn.ImageLayout = DataGridViewImageCellLayout.NotSet
imageColumn.Description = "default image layout"
dataGridView1.Columns.Add(imageColumn)
columnCount = columnCount + 1
Loop While columnCount < 3
End Sub