我想用换行符替换动态网格视图中的分号

时间:2011-10-10 19:28:09

标签: vb.net gridview replace

我刚刚开始进行VB.NET编程,并设计了一个带有下拉列表(从SQL动态读取)的简单网页,该网页填充了一个只显示该颜色公式的GridView。它涉及专业涂料的混色配方。公式本身具有由一列中的分号分隔的单个部分,我想将此字符用作换行符,以便每个公式部分显示在GridView单元格中的单独行上。我已经尝试了很多方法来替换,拆分等,但遇到了路障。在哪里/如何添加代码来执行此替换?

这是我的代码:

<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 />
            <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>

代码背后:

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

1 个答案:

答案 0 :(得分:0)

只是为了充实你的答案user988265我假设你的意思是使用代码:

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

当我需要类似的东西时,为我工作。想知道是否有可能填充下拉列表?