Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 22nd, 2005, 03:35 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default Displaying Boolean Values from Datagrid

Hi All,

Happy Holidays! I'm new to this group.

My problem is that I want to display Boolean values in a format other than True or False. I could run a function on those columns in an Item Template but the columns are being pulled dynamically.

I have a FOR loop in the code that I'm using to hide specific columns. I was wondering if there was some way to identify the datatype as Boolean in this FOR loop and then be able to specify a function to format the data. In this case I'd like to display an image of a check (check.gif) if it is True or nothing if its False.

Here's the FOR loop. Here I'm setting the ID column visible False.

        For i = 0 To oDS.Tables(0).Columns.Count - 1
            Dim objbc As New BoundColumn
            objbc.DataField = oDS.Tables(0).Columns(i).ColumnName
            objbc.HeaderText = oDS.Tables(0).Columns(i).ColumnName
            If objbc.DataField = oDS.Tables(0).Columns("InvID").ColumnName Then
                objbc.Visible = False
            End If
            grdInventory.Columns.Add(objbc)
        Next

Any help would be greatly appreciated.

Thanks,
Richard

 
Old December 25th, 2005, 02:46 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

You need to use the ItemDataBound event of the datagrid:
**Note, the column is a template column with the default label, and an added image control(visible = false)
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim lbl As New Label
            lbl = CType(e.Item.Cells(0).FindControl("label1"), Label)
            If lbl.Text = "True" Then
                lbl.Visible = False
                e.Item.Cells(0).FindControl("image1").Visible = True
            End If
        End If
    End Sub

HTML:

<asp:DataGrid id=DataGrid1 style="Z-INDEX: 101; LEFT: 152px; POSITION: absolute; TOP: 40px" runat="server" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellSpacing="1" BackColor="White" CellPadding="3" GridLines="None" DataSource="<%# Dsbool1 %>" AutoGenerateColumns="False">
                <FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
                <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#9471DE"></SelectedItemStyle>
                <ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
                <HeaderStyle Font-Bold="True" ForeColor="#E7E7FF" BackColor="#4A3C8C"></HeaderStyle>
                <Columns>
                    <asp:TemplateColumn HeaderText="bool">
                        <ItemTemplate>
                            <asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.bool") %>'>
                            </asp:Label>
                            <asp:Image id="Image1" runat="server" ImageUrl="check_red.gif" Visible="False"></asp:Image>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox id=TextBox1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.bool") %>'>
                            </asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateColumn>
                </Columns>
                <PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="#C6C3C6"></PagerStyle>
            </asp:DataGrid>

 
Old December 27th, 2005, 07:17 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Hi jbenson,

Thank you so much for your reply. With the holidays and all I haven't had a chance to implement your code and I'm going out of town for a few days. I will give it a shot as soon as I get back early next year. I've been searching for a few weeks now for a solution.

Thanks again. Best wishes for the new year.

Richard

 
Old December 29th, 2005, 06:07 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You can create a checkmark column with a simple template column and databinding:
<asp:templatecolumn>
    <itemtemplate>
        <asp:image runat="server" visible='<%# DataBinder.Eval(Container, "DataItem.EnableViewState") %>' imageurl="check.gif" />
    </itemtemplate>
</asp:templatecolumn>

You can put any evaluation in the databinding as long as it results in a boolean.

-Peter
 
Old January 3rd, 2006, 04:30 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Hi,

I can't use any ItemTemplate in the datagrid because I'm pulling the columns dynamically. Here's the closest I've gotten.

Private Sub grdInventory_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles grdInventory.ItemDataBound
        Select Case e.Item.ItemType
            Case ListItemType.AlternatingItem, ListItemType.Item
                If e.Item.GetType Is GetType(Boolean) Then
                    Response.Write("BAM!")
                    Response.End()
                End If
        End Select
    End Sub

I can't get it to go into the "IF" statement.

If I can identify the Boolean then I can do a REPLACE function to replace TRUE with whatever.

Thanks for your response.

 
Old January 3rd, 2006, 04:49 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

What do you mean you are pulling columns dynamically?

 
Old January 3rd, 2006, 05:48 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I had actually started to work on this problem to satisfy my own curiosity as I have the need for a similar solution.

You're getting there....

You can access the data that drives the grid item at e.Item.DataItem

http://msdn.microsoft.com/library/en...aitemtopic.asp

From there you should be able to figure out the data type. The key problem I haven't solved yet is determining how to look thru the data item. When you bind to a DataTable the data type of e.Item.DataItem is (I believe) DataRowView. From there you can see all the columns and such. So you may at that point be able to correlate the column in the DataRowView with the dynamic column in the datagrid and thus be able to manipulate the contents of the specific cell that matches up.

-Peter
 
Old January 3rd, 2006, 07:07 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Hi,

jbenson - We have to create inventories for a long list of rules for the California environmental reports. Each rule has different reporting requirements and different inventory styles. So we won't have to create a separate aspx page and manage the pages for each rule, we define the rule in a table, assign columms to use for each particular rule, provide a name for the column and use that definition to create inventories and reports, in other words to create our SQL statements, INSERT, EDIT, SELECT. We've looked at quite a few different options and this one will definitely be the best. Pretty much everything is working as far as creating the correct SQL statement but since we don't know which columns will be pulled for any rule at run time we can't create ItemTemplates. All of the columns that have straight ahead data or number values work fine but since we are also using Boolean values all we are getting written to the page is either True or False. I hope this makes sense. I know it probably seems like something simpler could be done. But since we're simple guys here we focus on the simple. Hah, sounds pretty complicated for simple guys trying to write simple code.

Really appreciate your input!

planoie - I checked out the link you sent. I'm going to run the code on that page and see what sense I can make of it.

Will let you know if I make any progress.

Thanks,
Richard







 
Old January 3rd, 2006, 10:09 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Made a little bit of progress. I can now identify the data type within the FOR loop:

     For i = 0 To oDS.Tables(0).Columns.Count - 1
                Dim oDC As New BoundColumn
                oDC.DataField = oDS.Tables(0).Columns(i).ColumnName
                oDC.HeaderText = oDS.Tables(0).Columns(i).ColumnName
                If oDC.DataField = "InvID" Then
                    oDC.Visible = False
                End If
                If oDS.Tables(0).Columns(i).DataType.Name = "Boolean" Then

                End If
                grdInventory.Columns.Add(oDC)
            Next
            For i = 0 To oDS.Tables(0).Rows.Count - 1
                For n = 0 To oDS.Tables(0).Columns.Count - 1
                    If oDS.Tables(0).Rows(i).Item(n).GetType Is GetType(Boolean) Then
                        If oDS.Tables(0).Rows(i).Item(n) Then
                            oDS.Tables(0).Rows(i).Item(n) = 1
                        Else
                            oDS.Tables(0).Rows(i).Item(n) = 0
                        End If
                    End If
                Next
            Next
        grdInventory.DataSource = oDS.Tables(0)
        grdInventory.DataBind()

            grdInventory.Columns.Add(objbc)
        Next

Now I'm not sure where to go from here. In the Boolean IF statement its not letting me cast that column value to anything but a Boolean value. So, it looks like I may need to convert that column data type to a string and then run a CASE statement or something to display "" for FALSE and check.gif if TRUE.

Thanks,
Richard

 
Old January 4th, 2006, 12:22 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

You can add template columns dynamically. I have done this in the past for a project using help from this article:

http://msdn.microsoft.com/library/de...ridcontrol.asp

Jim






Similar Threads
Thread Thread Starter Forum Replies Last Post
boolean value shows true and false in the datagrid debjanib ASP.NET 2.0 Basics 2 March 23rd, 2007 08:38 AM
Updating boolean values using datalist Scripts82 ASP.NET 1.0 and 1.1 Basics 1 September 20th, 2006 04:17 AM
How do I send boolean values with a checkbox babou SQL Server ASP 2 March 28th, 2005 05:43 AM
tables not displaying all the rows values... abhit_kumar JSP Basics 2 January 17th, 2005 12:03 AM
Displaying listbox values.....??? Brettvan1 VB.NET 2002/2003 Basics 4 December 13th, 2004 06:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.