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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:52:25+00:00 2026-06-05T10:52:25+00:00

Winforms .NET 3.5 (C#) I have a DataGridView (DGView) and I created CustomColumn and

  • 0

Winforms .NET 3.5 (C#)

I have a DataGridView (DGView) and I created CustomColumn and CustomCell to be displayed in the DGView. I created a CustomUserControl which I want to display in the CustomCell.

Problem: I don’t see the user control in the column. I think I need to override Paint() method in CustomCell – Any points how can I do that ?

Note – MSDN example of hosting user control is for editing the cell value – where you make your user control visible right where you are editing your cell. I want my user control to render as a normal winform control. This user control shows notifications for the row .. and each row can have different notifications. I want users to be able to click on notification and get more details about it. .. but for now I am stuck at “how do I display this user control”

Any pointers will be highly appreciated.

 public class CustomColumn : DataGridViewColumn {
    public CustomColumn() : base(new CustomeCell()) { }
    public override DataGridViewCell CellTemplate
    {
        get
        {
            return base.CellTemplate;
        }
        set
        {
            // Ensure that the cell used for the template is a CalendarCell.
            if (value != null &&
                !value.GetType().IsAssignableFrom(typeof(CustomeCell)))
            {
                throw new InvalidCastException("It should be a custom Cell");
            }
            base.CellTemplate = value;
        }
    }
}
public class CustomeCell : DataGridViewTextBoxCell
{
    public CustomeCell() : base() { }
    public override Type ValueType
    {
        get
        {
            return typeof(CustomUserControl);
        }
    }
    public override Type FormattedValueType
    {
        get
        {
            return typeof(CustomUserControl);
        }
    }
}
  • 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-05T10:52:26+00:00Added an answer on June 5, 2026 at 10:52 am

    First Try: I tried to place a user control on the grid where I needed. Problem: Scrolling the data grid view requires re arranging all those user controls. Result – Rejected.

    Second Try: I constructed a user control and painted it in the appropriate cell. Result – works so far.

    I just overrode Paint and OnClick methods of DataGridViewCell in the CustomCell class.

    public class CustomeCell : DataGridViewCell
    {
        public override Type ValueType
        {
            get { return typeof(CustomUserControl); } 
        }
    
        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)
        {
            var ctrl = (CustomUserControl) value;
            var img = new Bitmap(cellBounds.Width, cellBounds.Height);
            ctrl.DrawToBitmap(img, new Rectangle(0, 0, ctrl.Width, ctrl.Height));
            graphics.DrawImage(img, cellBounds.Location);
        }
    
        protected override void OnClick(DataGridViewCellEventArgs e)
        {
            List<InfoObject> objs = DataGridView.DataSource as List<InfoObject>;
            if (objs == null)
                return;
            if (e.RowIndex < 0 || e.RowIndex >= objs.Count)
                return;
    
            CustomUserControl ctrl = objs[e.RowIndex].Ctrl;
            // Take any action - I will just change the color for now.
            ctrl.BackColor = Color.Red;
            ctrl.Refresh();
            DataGridView.InvalidateCell(e.ColumnIndex, e.RowIndex);
        }
    }
    

    The example renders the CustomControl in the CustomCell of CustomColumn ;). When user clicks on the cell, CustomCell‘s OnClick handles the click. Ideally, I would like to delegate that click to the custom user control CustomControl – which should handle the event as if it was a click on itself (custom user control can internally host multiple controls) – so its little complex there.

    public class CustomColumn : DataGridViewColumn
    {
        public CustomColumn() : base(new CustomeCell()) { }
    
        public override DataGridViewCell CellTemplate
        {
            get { return base.CellTemplate; }
            set
            {
                if (value != null && !value.GetType()
                    .IsAssignableFrom(typeof (CustomeCell)))
                    throw new InvalidCastException("It should be a custom Cell");
                base.CellTemplate = value;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Microsoft Dynamic CRM. I have a .NET Winforms application which
I have a DataGridView (.NET winforms) bound to a collection. One of the fields
Project: Winforms, .NET framework: 3.5 (Visual Studio 2008) My problem is: I have a
i have a .net winforms application that i want to be soundless. occasionally when
I have a .NET Winforms app (created in VS2005) that I deploy using ClickOnce.
I have a datagridview in a c# .net WinForms application that captures order detail
I have a .net winforms app which has a few animation effects, fade ins
In a VB.NET Winforms application, I have a form that contains both a datagridview
I have a .net WinForms 2.0 application that has an image library of about
I have a .net winforms ListBox and I've added items to it using .Add().

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.