Add WebBrowser control to DataGridView cell

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??

Thanks!
MasterComputingAsked:
Who is Participating?
 
ulfurCommented:
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)
		{
			base.OnLoad(e);

			CreateWebBrowserCell("http://www.experts-exchange.com");

			CreateWebBrowserCell("http://www.w3schools.com");
		}


		private void CreateWebBrowserCell(string url)
		{
			WebBrowser browser = new WebBrowser();
			browser.Parent = dataGridView1;
			DataGridViewWebControlCell cell = new DataGridViewWebControlCell();
			cell.Value = browser;
			DataGridViewRow row = new DataGridViewRow();
			row.Cells.Add(cell);
			dataGridView1.Rows.Add(row);
			browser.Navigate(url);
		}

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
0
 
MasterComputingAuthor Commented:
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!
0
 
ulfurCommented:
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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.