Alex Zeller (DevExpress)
07.06.2005
You must
log in
or
register
to leave comments
1 Answer
We have prepared a special RepositoryItem, allowing you to accomplish this task. Please see the PictureEdit - How to display external images by providing links for them example.
Also, from version 3 onwards, the XtraGrid supports unbound columns. Unbound columns provide flexible solutions for various tasks that require arbitrary data to be displayed within the XtraGrid. In this case you can create an unbound column and use it to display external images.
The How to display external images in grid columns if the data source only contains links to the images example shows how to add an unbound column to the XtraGrid that will display external images. In this example an unbound column ("Image") is created. Its properties are set as follows:
FieldName to Image (the field name must be unique)
UnboundType to UnboundColumnType.Object (the column is supposed to display images)
ColumnEdit to an instance of the RepositoryItemPictureEdit class (this assigns a PictureEdit in-place editor to the column)
In addition a View's CustomUnboundColumnData event is handled to populate the unbound column with data. The event handler loads images, caches them in a hashtable and supplies them to the appropriate cells.
Important note:
Starting with version 11.1 we have introduced the e.Row object. It is a more correct and safe way to obtain values in the CustomUnboundColumnData event handler. So, if you are using version 11.1 and later, use the following code:
[C#]string colorName = (string)((DataRowView)e.Row)["Color"];
For more information, please refer to the following Knowledge Base article :
Why the CustomColumnDataEventArgs.RowHandle property is obsolete in the GridView.CustomUnboundColumnData event in version 11.1
[C#]string ImageDir = @"Images\"; Hashtable Images = new Hashtable(); string GetFileName(string color) { if(color == null || color == string.Empty) return string.Empty; return color + ".jpg"; } private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) { if(e.Column.FieldName == "Image" && e.IsGetData) { GridView view = sender as GridView; string colorName = (string)view.GetRowCellValue(e.RowHandle, "Color"); string fileName = GetFileName(colorName).ToLower(); if(!Images.ContainsKey(fileName)) { Image img = null; try { string filePath = DevExpress.Utils.FilesHelper.FindingFileName(Application.StartupPath, ImageDir + fileName, false); img = Image.FromFile(filePath); } catch { } Images.Add(fileName, img); } e.Value = Images[fileName]; } }
[VB.NET]Private ImageDir As String = "Images\" Private Images As Hashtable = New Hashtable() Private Function GetFileName(ByVal color As String) As String If color Is Nothing OrElse color = String.Empty Then Return String.Empty End If Return color & ".jpg" End Function Private Sub gridView1_CustomUnboundColumnData(ByVal sender As Object, ByVal e As _ DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) Handles gridView1.CustomUnboundColumnData If e.Column.FieldName = "Image" AndAlso e.IsGetData Then Dim view As GridView = TryCast(sender, GridView) Dim colorName As String = CStr(view.GetRowCellValue(e.RowHandle, "Color")) Dim fileName As String = GetFileName(colorName).ToLower() If (Not Images.ContainsKey(fileName)) Then Dim img As Image = Nothing Try Dim filePath As String = DevExpress.Utils.FilesHelper.FindingFileName(Application.StartupPath, ImageDir & fileName, False) img = Image.FromFile(filePath) Catch End Try Images.Add(fileName, img) End If e.Value = Images(fileName) End If End Sub
See Also:
How to put icons in grid cells
PictureEdit - How to display external images by providing links for them