ASP.NET - 我正在寻找一种将子字符串字体更改为网格视图单元格的方法
本文关键字:字体 字符串 网格 方法 单元格 视图 一种 NET 寻找 ASP | 更新日期: 2023-09-27 18:32:25
<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 />
所以这是我的网格视图:
<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 * 中提取,其中一列等于下拉列表中的选定项目,并且如果在描述单元格中包含描述文本字段中的子字符串。所以我希望当描述单元格出现在网格视图中时,文本字段中的子字符串要加粗。
这是我的按钮方法:
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;
}
}
像这样在你的绑定字段上设置htmlencode="false"
的第一组。
<asp:BoundField DataField="Description" HeaderText="Description"
htmlencode="false" SortExpression="Description" />
网格视图的行数据绑定中写以下内容。
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);
}