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 9038121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:24:46+00:00 2026-06-16T09:24:46+00:00

I have extended the DataGridView cell to display the text from its Tag property

  • 0

I have extended the DataGridView cell to display the text from its Tag property in the corner (e.g. displaying the day number in the corner of a calendar) and would like to be able to specify the Color and opacity of the text.

To accomplish this I have added 2 properties to the subclassed DataGridView cell, however they are not storing their values at runtime. This is the DataGridViewCell and Column:

class DataGridViewLabelCell : DataGridViewTextBoxCell
{
    private Color _textColor;
    private int _opacity;

    public Color TextColor { get { return _textColor; } set { _textColor = value; } }
    public int Opacity { get { return _opacity; } set { _opacity = value; } }

    protected override void Paint(Graphics graphics,
                                  Rectangle clipBounds,
                                  Rectangle cellBounds,
                                  int rowIndex,
                                  DataGridViewElementStates cellState,
                                  object value,
                                  object formattedValue,
                                  string errorText,
                                  DataGridViewCellStyle cellStyle,
                                  DataGridViewAdvancedBorderStyle advancedBorderStyle,
                                  DataGridViewPaintParts paintParts)
    {
        // Call the base class method to paint the default cell appearance.
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
            value, formattedValue, errorText, cellStyle,
            advancedBorderStyle, paintParts);

        if (base.Tag != null)
        {
            string tag = base.Tag.ToString();
            Point point = new Point(base.ContentBounds.Location.X, base.ContentBounds.Location.Y);
            Font font = new Font("Arial", 25.0F, FontStyle.Bold);
            graphics.DrawString(tag, font, new SolidBrush(Color.FromArgb(_opacity, _textColor)), cellBounds.X, cellBounds.Y);
        }
    }
}

public class DataGridViewLabelCellColumn : DataGridViewColumn
{
    public DataGridViewLabelCellColumn(Color TextColor, int Opacity = 128)
    {
        DataGridViewLabelCell template = new DataGridViewLabelCell();
        template.TextColor = TextColor;
        template.Opacity = Opacity;
        this.CellTemplate = template;
    }
}

I add the columns as follows:

col = new DataGridViewLabelCellColumn(Color.Blue, 115);
dgv.Columns.Add(col);
col.HeaderText = "Saturday";
col.Name = "Saturday";

However, if I add a breakpoint to the graphics.DrawString line neither _textColor nor _opacity have a value. If I assign them default values as follows:

private Color _textColor = Color.Red;
private int _opacity = 128;

Then it works fine. How can I ensure that the values get stored in the CellTemplate?

  • 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-06-16T09:24:48+00:00Added an answer on June 16, 2026 at 9:24 am

    I believe it’s because the CellTemplate is stored as a more generic DataGridViewCell, rather than the subclassed LabelCell. At any rate, storing the values on the Column and just referencing them from there works fine:

    DataGridViewLabelCellColumn clm = (DataGridViewLabelCellColumn)base.OwningColumn;
    int opacity = clm.Opacity;
    Color textColor = clm.TextColor;
    graphics.DrawString(tag, font, new SolidBrush(Color.FromArgb(opacity, textColor)), cellBounds.X, cellBounds.Y);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have extended a MaskedTextBox component to add some functionality. The text property of
I have extended my entities to implement specific interfaces for its type. I am
I have a class named CropImageView which is extended from ImageView . but the
I have extended the standard FileSystemWatcher class by adding a string property (fileToTest), now
I have extended a PictureBox and created a singleton. Is it possible to display
I have extended an Entity Framework 4 entity class with a calculated property in
I have extended the pages table with one text field named TypoScript . My
I have a problem when I want to transfer data from Excel to dataGridView
I have extended a RelativeLayout with all its constructor.And I have created a layout
I have extended org.eclipse.ui.editors.text.TextEditor to implement a custom editor. For this editor, I have

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.