ASP.NET - 我正在寻找一种将subString字体更改为gridview单元格的方法

时间:2016-01-28 11:02:44

标签: c# asp.net gridview

 <asp:TextBox ID="searchDescription" runat="server" Height="26px" Width="270px"></asp:TextBox>
        <br />
        <br />

        <asp:Button ID="Button2" runat="server" Text="Търси" onclick="Button2_Click" />
        <br />
        <br />

所以这是我的gridview:

<asp:GridView ID="GridView2" runat="server" HorizontalAlign="Center" AllowPaging="True" 
            AutoGenerateColumns="False" CellPadding="4"
            ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:BoundField DataField="Марка" HeaderText="Марка" SortExpression="Марка" />
                <asp:BoundField DataField="Година" HeaderText="Година" 
                    SortExpression="Година" />
                <asp:BoundField DataField="Мощност" HeaderText="Мощност" 
                    SortExpression="Мощност" />
                <asp:BoundField DataField="Вносител" HeaderText="Вносител" 
                    SortExpression="Вносител" />
                <asp:BoundField DataField="Description" HeaderText="Description" 
                    SortExpression="Description" />
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>

我有一个描述文本字段和DropDownList以及一个按钮,该按钮生成一个查询,该查询从表中的DB *中取出,其中一列等于DropDown中的所选项,如果在描述单元格中包含描述文本字段中的subString。所以我想当描述单元格出现在gridView中时,来自文本字段的subString要加粗。

这是我的按钮方法:

protected void Button2_Click(object sender, EventArgs e)
    {
        string srchDescription = searchDescription.Text;
        string slctItem = DropDownList1.SelectedItem.ToString();

        con.Open();

        SqlCommand cmdForDescription = new SqlCommand("SELECT Description FROM car_table WHERE Вносител = '" + slctItem + "'", con);
        string descriptionText = cmdForDescription.ExecuteScalar().ToString();
        bool containsDescription = descriptionText.Contains(srchDescription);

        if (containsDescription)
        {

            SqlCommand cmd = new SqlCommand("SELECT * FROM car_table WHERE Вносител = '" + slctItem + "'", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView2.DataSource = ds;
            GridView2.DataBind();
            con.Close();
            Label2.Visible = true;

        }
    } 

1 个答案:

答案 0 :(得分:0)

首先在你的绑定字段上设置htmlencode="false"

<asp:BoundField DataField="Description" HeaderText="Description" 
                 htmlencode="false"   SortExpression="Description" />

你的gridview的RowDataBound写下来。

if(e.Row.RowType == DataControlRowType.DataRow)
{
   string textToReplace = searchDescription.Text;
   string newText = "<b>" +searchDescription.Text + "</b>";
   e.Row.Cells[4].Text =e.Row.Cells[4].Text.Replace(textToReplace, newText);    
}