Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 359071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:21:33+00:00 2026-05-12T12:21:33+00:00

I would like to create a control that extends the BoundField that’s used within

  • 0

I would like to create a control that extends the BoundField that’s used within a GridView. What I’d like to do is provide another property named HighlightField that will be similar to the DataField property in that I want to give it the name a data column. Given that data column it would see if the value is true or false and highlight the given text within the given column on the given row.

Some psuedo-code if that doesn’t make sense:

<asp:GridView id="grid">
  <Columns>
    <asp:BoundField DataField="Name" />
    <cc:HighlightField DataField="Name" HighlightField="IsHighlighted" />
  </Columns>
</asp:GridView>

And then within the databind or something:

if(this row's IsHighlighted value is true)
  set the CssClass of this datacell  = "highlighted"
(or wrap a span tag around the text)

Ravish pointed me in the correct direction, here’s what I ended up with:

public class HighlightedBoundField : BoundField
{
    public string HighlightField
    {
        get { return ViewState["HighlightField"].ToString(); }
        set
        {
            ViewState["HighlightField"] = value;
            OnFieldChanged();
        }
    }

    public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
    {
        base.InitializeCell(cell, cellType, rowState, rowIndex);

        bool isDataRowAndIsHighlightFieldSpecified = cellType == DataControlCellType.DataCell && !string.IsNullOrEmpty(HighlightField);
        if (isDataRowAndIsHighlightFieldSpecified)
        {
            cell.DataBinding += new EventHandler(cell_DataBinding);
        }
    }

    void cell_DataBinding(object sender, EventArgs e)
    {
        TableCell cell = (TableCell)sender;
        object dataItem = DataBinder.GetDataItem(cell.NamingContainer);
        cell.Text = DataBinder.GetPropertyValue(dataItem, DataField).ToString();

        bool highlightThisCellsText = Convert.ToBoolean(DataBinder.GetPropertyValue(dataItem, HighlightField));
        if (highlightThisCellsText)
        {
            cell.CssClass += " highlight";
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T12:21:33+00:00Added an answer on May 12, 2026 at 12:21 pm

    Untested:

    public class HighlightBoundField : DataControlField {
    
        //property to indicate if this field should be highlighted, given the value of this property
        //
        public string HighlightField {
            get {
                object value = ViewState["HighlightField"];
    
                if (value != null) {
                    return Convert.ToString(value);
                }
    
                return "";
            }
    
            set {
                ViewState["HighlightField"] = value;
                OnFieldChanged();
            }
        }
    
        //property to display as text in the cell
        //
        public string DataField {
            get {
                object value = ViewState["DataField"];
    
                if (value != null) {
                    return value.ToString();
                }
    
                return string.Empty;
            }
    
            set {
                ViewState["DataField"] = value;
    
                OnFieldChanged();
            }
        }
    
        //bound field creation
        //
        protected override DataControlField CreateField() {
            return new BoundField();
        }
    
        //override the method that is used to populate and format a cell
        //
        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex) {
            base.InitializeCell(cell, cellType, rowState, rowIndex);
    
            //if this celltype is a data row
            //
            if (cellType == DataControlCellType.DataCell && !string.IsNullOrEmpty(HighlightField)) {
                //create label control to display text
                //
                var lblText = new Label();
    
                //add event listener for when the label is bound
                //
                lblText.DataBinding += new EventHandler(lblText_DataBinding);
    
                //add label to controls collection
                //
                cell.Controls.Add(lblText);
            }
        }
    
        void lblText_DataBinding(object sender, EventArgs e) {
            //retrieve data item and set label text
            //
            Label lblText = (Label) sender;
            object dataItem = DataBinder.GetDataItem(lblText.NamingContainer);
            lblText.Text = DataBinder.GetPropertyValue(dataItem, DataField).ToString();
    
            //check if value should be highlighted
            //
            if (Convert.ToBoolean(DataBinder.GetPropertyValue(dataItem, HighlightField))) {
                lblText.Style.Add("background-color", "yellow");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 184k
  • Answers 184k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can make Postcode into a property that returns a… May 12, 2026 at 4:47 pm
  • Editorial Team
    Editorial Team added an answer One option that will surely work is to replace Int*… May 12, 2026 at 4:47 pm
  • Editorial Team
    Editorial Team added an answer I was able to reproduce this by following your instructions,… May 12, 2026 at 4:46 pm

Related Questions

I would like to create a control that extends the BoundField that's used within
I have a Set class (This is J2ME, so I have limited access to
I dont think this is quite possible, but its worth a shot to see
I would like to create a simple control that inherits from HeaderedContentControl, and has
My goal is to create a composite control that looks like, acts like and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.