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

  • Home
  • SEARCH
  • 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

Related Questions

I would like to create (in code) tree of controls that would render as
I am trying to create a winForms user control. But I want would like
I would like create my own collection that has all the attributes of python
I would like create a web service in ASP.Net 2.0 that will supports JSON.
I would like to create a stored procedure in MySQL that took a list
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to create a database backed interactive AJAX webapp which has a
I would like to create an SSL connection for generic TCP communication. I think
I would like to create a file format for my app like Quake, OO,
I would like to create an application wide keyboard shortcut for a Java Swing

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.