简体   繁体   中英

Make part of DataRow Cell as Bold in DataGrid codebehind C#

How can i make part of a text inside Cell of Data Grid Row as Bold. I am able to make whole cell text as Bold by following code.

void Item_Bound(Object sender, DataGridItemEventArgs e)
{
    e.Item.Cells[0].Font.Bold = true;
}

I am creating Datagrid from code behind so can't use any aspx page options

Well you could pull out the text and replace it with a span with the bold option turned on.

I would do this for a specific control like a Label or Literal (use a TemplateColumn if required) and use the OnDataBinding method of the control as the OnDataBound event in my opinion is not the appropriate place to do so.

But to use your example above you could do the following:

void Item_Bound(Object sender, DataGridItemEventArgs e)
{
    string yourSubString = "some string to bold";
    e.Item.Cells[0].Text = e.Item.Cells[0].Text.Replace(yourSubString,
        string.Format("<span style='font-weight: bold'>{0}</span>", yourSubString));
}

The above should replace whatever string you define in yourSubString with the bold version. Keep in mind if it appears multiple times, all instances will be replaced. If you only want to do one version you will have to call the replace and make sure to SubString out the rest before doing the replace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM