开发者

I want to replace semicolons in a dynamic gridview with a line break

开发者 https://www.devze.com 2023-04-12 14:51 出处:网络
I\'m just starting into VB.NET programming, and have designed a simple webpage with a dropdown list (dynamically read from SQL) that populates a GridView showing only that color\'s formula. It deals w

I'm just starting into VB.NET programming, and have designed a simple webpage with a dropdown list (dynamically read from SQL) that populates a GridView showing only that color's formula. It deals with color-mixing formulas for specialized paint. The formulas themselves have it's inidividual parts separated by semi-colons in one of the columns, and I want to use this character as a line-break, so that each formula part is shown on a seperate line within the GridView cell. I have tried numerous ways to Replace, Split, etc, but running into a road-block. Where/how do I add code to perform this replacement?

Here's my code as it sits:

<body> 
    <form id="form1" runat="server">
        <div> 
            <asp:ImageButton ID="ImageButton1" runat="server" 
                             AlternateText="Home Page" Height="46px" 
                             ImageUrl="~/Images/Block_Tagline.jpg"
                             PostBackUrl="~/Default.aspx" Width="140px" />
            <br />
            <asp:LoginName ID="LoginName" 
                           FormatString="Currently logged in as {0}" 
                           runat="server" 
                           ForeColor="#009933" />
            <br />
            <br />
            <br />
            Choose Your Color:
            <asp:DropDownList ID="DropDownList1" runat="server" 
                              AutoPostBack="true" 
                              OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            </asp:DropDownList>
            <br />
            <br />
            开发者_StackOverflow中文版<asp:GridView ID="ColorGridView" runat="server" 
                          Width="100%" BackColor="White" 
                          BorderColor="#E7E7FF" BorderStyle="None" 
                          BorderWidth="1px" CellPadding="3" 
                          GridLines="Horizontal">
                <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                <PagerStyle ForeColor="#4A3C8C" HorizontalAlign="Right"
                            BackColor="#E7E7FF" />
                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True"
                                  ForeColor="#F7F7F7" />
                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" 
                             ForeColor="#F7F7F7" />
                <AlternatingRowStyle BackColor="#F7F7F7" /> 
            </asp:GridView>
            <span id="Message" runat="server"/>
            <b></b>
            <br />
            <asp:Button ID="btnEdit" runat="server" 
                        Text="Edit/Add/Delete Colors" 
                        PostBackUrl="~/Edit/EditColors.aspx" />
        </div>
    </form>
</body>

Code Behind:

Partial Class _Default
        Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Dim username As String = System.Security.Principal.WindowsIdentity.GetCurrent().Name
            DropDownList1.Items.Insert(0, New ListItem("---Select---"))

            FillDropDownList()
        End If
    End Sub

    ' Show data in GridView
    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

        Dim s As String = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim con As SqlConnection = New SqlConnection(s)

        con.Open()

        Dim cmd As SqlCommand = New SqlCommand("SELECT * from z_md_ColorFormulas where BlankColor='" + DropDownList1.SelectedItem.ToString() + "'", con)
        Dim dr As SqlDataReader = cmd.ExecuteReader()

        ColorGridView.DataSource = dr
        ColorGridView.DataBind()

        dr.Close()
        con.Close()
    End Sub

    Public Sub FillDropDownList()

        Dim s1 As String = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim con1 As SqlConnection = New SqlConnection(s1)

        con1.Open()

        Dim cmd1 As SqlCommand = New SqlCommand("Select BlankColor from z_md_ColorFormulas", con1)
        Dim dr1 As SqlDataReader = cmd1.ExecuteReader()

        While dr1.Read()
            DropDownList1.Items.Add(dr1(0).ToString())
        End While

        dr1.Close()
        con1.Close()
    End Sub


Just to flesh out your answer user988265 I assume you meant using the code:

<asp:TemplateField HeaderText="Colours">
    <ItemTemplate>
        <%# Eval("Colour").ToString().Replace(";","<br />") %>
    </ItemTemplate>
</asp:TemplateField>

Worked for me when I needed something similar. Was wondering is it possible to have this populate a drop-down list though?

0

精彩评论

暂无评论...
验证码 换一张
取 消