Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Add WebBrowser control to DataGridView cell

Posted on 2011-04-21
3
Medium Priority
?
2,717 Views
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??

Thanks!
1
Comment
Question by:MasterComputing
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
ulfur earned 2000 total points
ID: 35445606
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
 

Author Comment

by:MasterComputing
ID: 35449293
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
 
LVL 1

Expert Comment

by:ulfur
ID: 35451188
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

564 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