Hey, I am suprised noboby has posted the answer to this yet. Off the
top of my head I can think of three ways you can do this:
1. Modify the sql code to return the image, example: case val when 1
then 'true image' else 'false image' end
2. Dump the contents into a datatable (or array) and loop through the
contents to change the values then assign it to the datagrid.
3. Use the ItemDataBound event of the datagrid object while the object
is being rendered. This would be my choice.
The key is the ItemDataBound event fires for every row that is bound
to the datagrid. Here you have a chance to modify the row before it
is displayed. Anyway, here is the datagrid method to get you started.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
'for testing, put database retrieval code here
Dim t As DataTable = New DataTable
t.Columns.Add("invoice")
t.Columns.Add("val1")
t.Columns.Add("val2")
t.Columns.Add("val3")
t.LoadDataRow(New Object() {"1", "1", "1", "0"}, True)
t.LoadDataRow(New Object() {"2", "0", "1", "0"}, True)
t.LoadDataRow(New Object() {"3", "1", "0", "1"}, True)
Me.DataGrid1.DataSource = t 'could also be a sqldatareader
Me.DataGrid1.DataBind()
End If
End Sub
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
Dim i As Integer
If (e.Item.ItemIndex <> -1) Then 'skip the header row
For i = 1 To e.Item.Cells.Count - 1 'check and covert all
columns but the first
If e.Item.Cells(i).Text = "1" Then
e.Item.Cells(i).Text = "true image"
Else
e.Item.Cells(i).Text = "false image"
End If
Next
End If
End Sub |