change font color if data is 1 and display X instead of true

Last post 12-27-2006 10:20 AM by dzirkelb. 2 replies.

Sort Posts:

  • change font color if data is 1 and display X instead of true

    12-22-2006, 5:10 PM
    • Member
      364 point Member
    • dzirkelb
    • Member since 05-25-2005, 1:11 PM
    • Cedar Rapids
    • Posts 128

    I have a page which has the option for a user to select two different search options.  I only have working the MFG option this far, so please ignore the Rep portion.

     I display the results in a datagrid...all works fine so far; however, I wish to add some functionality. First, I have numerous bit fields in my sql 2000 database for each state...if the data is 1, then it displays true on the page.  I wish, instead of displaying true, that it would display a Green X, and if it is false (0), then display nothing in the field.  Currently, if it is 0, then it displays false.  How with my existing code can I accomplish this?  Here is my code in its entireity.

     I will not be back to work until Tuesday of next week, so I will not be able to test anything until then...thanks!

     

    <%

    @ Page Debug="true" %>

    <

    script runat="server">

    Sub SearchButtonClick(ByVal s As Object, ByVal e As EventArgs)

    Dim conn As SqlConnection

    Dim cmdSelect As SqlCommand

     

    conn =

    New SqlConnection("Data Source=CDRSRV4;Initial Catalog=ECIS;Persist Security Info=True;User ID=sa;Password=sa")

    If radMfg.Checked Then

    cmdSelect =

    New SqlCommand("SELECT RecordID, RepCompany, IA, NE, MN, WI, IL, SD, ND, MO, KS, AR FROM MfgRepList WHERE (Mfg = @Mfg)", conn)

    cmdSelect.Parameters.AddWithValue(

    "@Mfg", txtSearch.Text)

    conn.Open()

    dgrdMfg.DataSource = cmdSelect.ExecuteReader

    dgrdMfg.DataBind()

    conn.Close()

    Else

    cmdSelect =

    New SqlCommand("SELECT RecordID, Mfg, FROM MfgRepList WHERE (RepCompany = @RepCompany)", conn)

    cmdSelect.Parameters.AddWithValue(

    "@RepCompany", txtSearch.Text)

    End If

    End Sub

    </

    script>

     

    <

    html xmlns="http://www.w3.org/1999/xhtml" >

    <

    head runat="server">

    <title>Untitled Page</title>

    </

    head>

    <

    body bgcolor="silver">

    <form id="form1" runat="server">

    <asp:RadioButton ID="radMfg" GroupName="radSearch" Text="Mfg" runat="server" />

    <asp:RadioButton ID="radRep" GroupName="radSearch" Text="Rep" runat="server" />

    <p></p>

    <asp:TextBox ID="txtSearch" runat="server" />

    <asp:Button Text="Search" OnClick="SearchButtonClick" runat="server" />

    <p></p>

     

    <asp:DataGrid ID="dgrdMfg" DataKeyField="RecordID" AutoGenerateColumns="false" CellPadding="5" runat="server">

    <Columns>

    <asp:BoundColumn HeaderText="Rep Company" DataField="RepCompany" />

    <asp:BoundColumn HeaderText="IA" DataField="IA" />

    <asp:BoundColumn HeaderText="NE" DataField="NE" />

    <asp:BoundColumn HeaderText="MN" DataField="MN" />

    <asp:BoundColumn HeaderText="WI" DataField="WI" />

    <asp:BoundColumn HeaderText="IL" DataField="IL" />

    <asp:BoundColumn HeaderText="SD" DataField="SD" />

    <asp:BoundColumn HeaderText="ND" DataField="ND" />

    <asp:BoundColumn HeaderText="MO" DataField="MO" />

    <asp:BoundColumn HeaderText="KS" DataField="KS" />

    <asp:BoundColumn HeaderText="AR" DataField="AR" />

    </Columns>

     

    </asp:DataGrid>

    </form>

    </

    body>

    </

    html>

     

  • Re: change font color if data is 1 and display X instead of true

    12-22-2006, 11:14 PM
    Answer
    • Contributor
      3,859 point Contributor
    • rojay12
    • Member since 04-03-2006, 10:43 PM
    • Sacramento, CA
    • Posts 703

    Here are you options, do it in sql before you bring the data back with a sql statement like this...

    SELECT RecordID, RepCompany,
    Case IA When 1 Then 'X' Else null End,
    Case NE When 1 Then 'X' Else null End,
    Case MN When 1 Then 'X' Else null End,
    Case WI When 1 Then 'X' Else null End,
    Case SD When 1 Then 'X' Else null End,
    Case ND When 1 Then 'X' Else null End,
    Case MO When 1 Then 'X' Else null End,
    Case KS When 1 Then 'X' Else null End,
    Case AR When 1 Then 'X' Else null End 
    FROM MfgRepList WHERE (Mfg = @Mfg)

     

    Or make the  BoundColumn a TemplateColumn

    <asp:GridView ID="GridView1" runat="server">
          <Columns>
          <asp:TemplateField>
          <ItemTemplate>
          <asp:Label runat="server" ID="lblIA" />
          </ItemTemplate>
          </asp:TemplateField>
          </Columns>
    </asp:GridView>

    and then do this in the itemdatabound event

      Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
          CType(e.Row.FindControl("lblIA"), Label).Text = IIf(e.Row.DataItem("IA"), "X", "")
          'Repeat For All
        End If
      End Sub

    Jared Roberts
    Lead Application Developer
  • Re: change font color if data is 1 and display X instead of true

    12-27-2006, 10:20 AM
    • Member
      364 point Member
    • dzirkelb
    • Member since 05-25-2005, 1:11 PM
    • Cedar Rapids
    • Posts 128
    I used the sql portion from above and it worked great, thanks!
Page 1 of 1 (3 items)