gchq
Well-known member
- Joined
- Dec 14, 2007
- Messages
- 168
- Programming Experience
- 10+
I have tried every which way to get an image into a DataGrid... Any ideas where I am going wrong?
Thanks
(I know the variable is called DGV - winforms habit, but it is a WPF DataGrid)
Thanks
(I know the variable is called DGV - winforms habit, but it is a WPF DataGrid)
VB.NET:
Public Class DGVx
Inherits DataGrid
Public Overrides Sub BeginInit()
MyBase.BeginInit()
End Sub
Protected Overrides Sub OnInitialized(e As System.EventArgs)
MyBase.OnInitialized(e)
HorizontalAlignment = Windows.HorizontalAlignment.Stretch
VerticalAlignment = Windows.VerticalAlignment.Stretch
CanUserAddRows = False
CanUserDeleteRows = False
IsReadOnly = True
UnselectAll()
UnselectAllCells()
CurrentItem = Nothing
AlternatingRowBackground = RowColours(True)
GridLinesVisibility = DataGridGridLinesVisibility.None
Margin = New Thickness(3)
End Sub
End Class
VB.NET:
Private Sub ImagesItems_LoadData_Completed(ByVal sender As Object, ByVal e As ReturnDataSetHASCompletedEventArgs)
Try
If Not e.Cancelled Then
Dim DGV As CustomControl.DGVx = ImagesItemsGrid.FindName("ImagesItems_DGV")
Dim DS As DataSet = e.Result
Dim DT As DataTable = DS.Tables(0).Copy
'Configure for images in DGV
Dim vDT As New DataTable
With vDT.Columns
.Add("ID", GetType(Integer))
.Add("Name", GetType(String))
.Add("Description", GetType(String))
.Add("Image", GetType(Image))
End With
For Each Row As DataRow In DT.Rows
With vDT.Rows
Dim ImageData As Byte() = DirectCast(Row("Image_Icon"), Byte())
Dim vImage As New Image
Dim vBitMap As New BitmapImage
Using vStream As New IO.MemoryStream(ImageData)
vImage.Source = BitmapFrame.Create(vStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad)
End Using
vImage.Height = 20
.Add(Row("Image_ID"), Row("Image_Name"), Row("Image_Description"), vImage)
End With
Next
DT.Dispose()
For Each Col As DataColumn In vDT.Columns
Dim vDataType As String = Col.DataType.ToString
Select Case vDataType
Case "System.Int32"
DGV.Columns.Add(New DataGridTextColumn With {.Header = Col.ColumnName, .Binding = New Binding(String.Format("[{0}]", Col.ColumnName))})
Case "System.Windows.Controls.Image"
Dim ImageCol As New DataGridTemplateColumn
ImageCol.Header = Col.ColumnName
Dim vFactory As New FrameworkElementFactory(GetType(Image))
Dim vBinding As New Binding(String.Format("[{0}]", Col.ColumnName))
vBinding.Mode = BindingMode.OneWay
vFactory.SetValue(Image.SourceProperty, vBinding)
Dim vCellTemplate As New DataTemplate
vCellTemplate.VisualTree = vFactory
ImageCol.CellTemplate = vCellTemplate
DGV.Columns.Add(ImageCol)
'DGV.Columns.Add(New DataGridTemplateColumn With {.Header = Col.ColumnName, .
Case Else
' DGV.Columns.Add(New DataGridTextColumn With {.Header = Col.ColumnName, .Binding = New Binding(String.Format("[{0}]", Col.ColumnName))})
Dim DGTC As New DataGridTextColumn
With DGTC
.Header = Col.ColumnName
.Binding = New Binding(String.Format("[{0}]", Col.ColumnName))
End With
DGV.Columns.Add(DGTC)
End Select
Next
DS.Dispose()
DGV.DataContext = vDT
DGV.ItemsSource = vDT.DefaultView
End If
Catch ex As Exception
EmailError(ex)
End Try
End Sub