Google Answers Logo
View Question
 
Q: datagrid webform asp.net vb ( No Answer,   1 Comment )
Question  
Subject: datagrid webform asp.net vb
Category: Computers > Programming
Asked by: pbarron-ga
List Price: $10.00
Posted: 21 Mar 2005 15:05 PST
Expires: 20 Apr 2005 16:05 PDT
Question ID: 498270
I am developing an .aspx web page using MS vstudio net (a vb.net
form). I am retrieving data from an sql database where the first colum
is the Id for the row (i.e.  the invoice number) and the rest of the
colums (10 colums) are either true or false values.  My question is: 
I would like to display a checkitem image for the true values and a
red cross image for the false values in the datagrid.  How do I do
this?

Regards,
Paulo

Clarification of Question by pbarron-ga on 28 Mar 2005 07:08 PST
Dear jxsmith/google answers community:

What I am trying to accomplish is to return a gif image should the
database value be true and a different gif image for the false case
inside of the datagrid.

Jxsmith thank you for your comments.

Paulo
Answer  
There is no answer at this time.

Comments  
Subject: Re: datagrid webform asp.net vb
From: jxsmith-ga on 25 Mar 2005 09:29 PST
 
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

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy