Add WebBrowser control to DataGridView cell

Posted on 2011-04-21
1 Endorsement
Last Modified: 2013-12-16
I'm just cutting my teeth in winforms programming.  Most of my experience is in ASP.NET web programming.  I'm building an application that will display work orders from the website within a winform.  On the web, I use a GridView to display the work order notes (which contain HTML markup).  In the WinForms app, I am trying to use a DataGridView control to display the same info, pulled from the same SQL database.  The DataGridView control spits out raw HTML which is very user un-friendly.  I have yet to figure out how to get the DataGridView to display formatted text.

So... My next idea is to embed a webbrowser control in a cell on each row, and fill that with the HTML.  Is it possible to programatically add a webbrowser control within a cell of a DataGridView?  If not, any other ideas??

Question by:MasterComputing
    LVL 1

    Accepted Solution

    This should get you started:
    create a new Windows Forms project
    add a DataGridView control to your form
    create a custom DataGridViewCell
    public class DataGridViewWebControlCell : DataGridViewCell
    		private static readonly Type formattedValueType = typeof(string);
    		private static readonly Type valueType = typeof(WebBrowser);
    		public DataGridViewWebControlCell()
    		public override Type ValueType
    			get { return valueType; }
    		public override System.Type FormattedValueType
    			get { return formattedValueType; }
    		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)
    			if (rowIndex > -1 && value != null)
    				WebBrowser browser = value as WebBrowser;
    				browser.SetBounds(cellBounds.Left, cellBounds.Top, cellBounds.Width, cellBounds.Height, BoundsSpecified.All);
    			base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

    Open in new window

    add a custom DataGridViewColumn
    public class DataGridViewWebControlColumn : DataGridViewColumn
    		public DataGridViewWebControlColumn()
    			: base(new DataGridViewWebControlCell())

    Open in new window

    add the following code in Form1.cs to see some sample data
    protected override void OnLoad(EventArgs e)
    		private void CreateWebBrowserCell(string url)
    			WebBrowser browser = new WebBrowser();
    			browser.Parent = dataGridView1;
    			DataGridViewWebControlCell cell = new DataGridViewWebControlCell();
    			cell.Value = browser;
    			DataGridViewRow row = new DataGridViewRow();

    Open in new window

    A complete solution should account for all types of interaction, data binding, possibly editing the Url, and so on.
    I hope this helps

    Author Comment

    Ulfur: Definitely on the right track.  Now... Any ideas how to add that DataGridViewWebControlCell into an existing databound DataGridView?  I can unbind the data, and do it all manually.  But if you have a quick trick for adding it to an existing row within an existing Grid, you would truly be my hero!

    Either way I really appreciate your time and will accept your solution as complete.  If I don't hear back by day's end, I'll close this one out.  Thanks again!
    LVL 1

    Expert Comment

    Take a look at the code of any of the DataGridView????Cell types in System.Windows.Forms (use ILSpy or Reflector for this.) You will find complete implementations that can guide you.
    Have a great weekend!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now