3
I am generating a report and exporting to Excel, but as most reports the number of columns is dynamic and I am mounting the gridview dynamically. I need to demonstrate a column that displays images in the table, so I am including this column after having the result of my query, so far so good, but when I give the databind() this column that is in the datasource of gridview is not including in the control structure of gridview, thus not rendering and not demonstrating in the spreadsheet. Knowing that I store the images in the database in an array of bytes. Follow the snippet of my code below. Please help me!
Dim grid As New GridView
If posicaoLogo = -1 Then
'Remove coluna identificador
dataTable.Columns.RemoveAt(0)
grid.DataSource = New DataView(dataTable)
grid.DataBind()
Else
Dim testeGrid As DataView = New DataView(dataTable)
'Tratamento Para inclusão da coluna tipo byte() devido a logomarca
Dim row As DataRow
Dim column As DataColumn
ReDim listaLogo(0)
ReDim listaIdentificadorMarca(0)
i = 0
For Each row In testeGrid.Table.Rows
ReDim Preserve listaLogo(i)
ReDim Preserve listaIdentificadorMarca(i)
For Each column In testeGrid.Table.Columns
If column.ToString = "Identificador" Then
listaIdentificadorMarca(i) = row(column).ToString
End If
If column.ToString = "Logo" Then
If row(column).ToString = "SIM" Then
listaLogo(i) = "1"
Else
listaLogo(i) = "0"
End If
End If
Next column
i = i + 1
Next row
Dim dtcloned As Data.DataTable = testeGrid.Table.Clone
Dim logomarcaMarca As DataColumn = New DataColumn("Logomarca")
logomarcaMarca.DataType = GetType(System.Byte())
dtcloned.Columns.Add(logomarcaMarca)
i = 0
Dim dtMarca As Data.DataTable = testeGrid.Table.Copy
Dim logomarcaMarcaAux As DataColumn = New DataColumn("Logomarca")
logomarcaMarcaAux.DataType = GetType(System.Byte())
dtMarca.Columns.Add(logomarcaMarcaAux)
For Each rowData As DataRow In dtMarca.Rows
If listaLogo(i) = "1" Then
marca.identificador = listaIdentificadorMarca(i)
marca = marca.getImagem
rowData.SetField(dtMarca.Columns(dtMarca.Columns.Count - 1), marca.imagem)
dtcloned.ImportRow(rowData)
Else
dtcloned.ImportRow(rowData)
End If
i = i + 1
Next
'Remove coluna de controle
dtcloned.Columns.RemoveAt(dtMarca.Columns.Count - 2)
'Remove coluna identificador
dtcloned.Columns.RemoveAt(0)
Dim dataviewMarcaAux As New DataView(dtcloned)
grid.DataSource = dataviewMarcaAux
grid.DataBind()
End If
It makes me wonder, is it even possible to display an image just by passing the byte array directly to logoMarch and adding the column? Does this model really work? Check out this Stack post about this: http://stackoverflow.com/questions/17351045/showing-image-in-gridview-from-byte-array
– rodrigorf
So far I have not succeeded in passing an array of bytes or even the image itself, converting the array to System.Drawing.Image. I’m trying to search the forums somehow, but I found nothing that came in handy.
– user2992172
I will try to reproduce your example and comment here
– rodrigorf
Thank you! Waiting for your comment.
– user2992172