【问题标题】:Bold a string in c#在c#中加粗一个字符串
【发布时间】:2014-12-22 11:18:06
【问题描述】:

我有一个字符串,我需要添加方括号并将字体更改为粗体。我也试过这个,它没有按预期工作。

string strID = "<b>" + strGroupID + "-</b>";

现在这个字符串产生类似Nestle-的结果,结果应该是这样的[Nestle]-

非常感谢您对此的任何帮助。

代码

protected void gvComm_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
            string strGroupID = ((DropDownList)gvComm.FooterRow.FindControl("ddlKey")).SelectedValue;
            string strComm = ((TextBox)gvComm.FooterRow.FindControl("lblOwner")).Text.Trim();
            string strLeader = ((TextBox)gvComm.FooterRow.FindControl("lblIT")).Text.Trim();
            string strName = ((TextBox)gvComm.FooterRow.FindControl("lblFTC")).Text.Trim();              
            string strID = "<b>" + strGroupID + "-</b>";
            bool success = false;
            success = InsertComm(strGroupID , strComm, strLeader, strName , strID);                
            if (success)
            {
                ClientScript.RegisterStartupScript(Page.GetType(), "alert", "alert('Data Inserted Successfully');window.location='Mapping.aspx';", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(Page.GetType(), "alert", "alert('Insert Operation Failed');window.location='Mapping.aspx';", true);
            }
        }
    }

public static bool InsertComm(string strGroupID , string strComm, string strLeader, string strName , string strID)
    {
        bool success = false;
        string strcon = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        using (SqlConnection connection = new SqlConnection(strcon))
        {
            connection.Open();
            using (SqlCommand oCommand = connection.CreateCommand())
            {
                oCommand.CommandText = "insert into Comm values ('" + strGroupID + " " + 'N/A' + "','" + strComm+ "','" + strLeader+ "','" + strName + "','" + strID+ "')";
                oCommand.ExecuteNonQuery();
                success = true;
            }
        }
        return success;
    }

如果您看到 strGroupID,我将传递一个 N/A 值。例如:GroupID 列值应该是这样的

  [Nestle] - N/A where `[Nestle] -` only should be bold.

【问题讨论】:

  • 您打算在哪里使用此代码?
  • 我正在使用此代码在 gridview 中插入字段值。
  • 您不能将字符串设为粗体。某些显示为粗体的文本仅在特定 UI 的上下文中才有意义。例如。在网页上,您将使用 HTML CSS,但 WPF 视图上的标签将完全不同。您需要提供 UI 的详细信息。
  • 可能是gridview上应用的css不允许加粗。这个问题可能有很多原因。
  • 是的,那你是怎么展示的呢?加载到任何控件,如Label?还是来自数据库?你能在显示Nestle-的地方显示html/aspx吗?

标签: c# asp.net .net


【解决方案1】:

您不能将字符串设为粗体。你可以做的是控制你用来显示字符串粗体。

如果这是 gridview,您可以使用 grd_RowDataBound 命令将特定单元格设为粗体,例如 如果您想要加粗的值存在于单元格 [0] 中:

e.Row.Cells[0].Font.Bold = true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多