Click here to Skip to main content
15,910,358 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How can we pass an html table contents to a datagridview in c# windows application when table id not present ?
Thanks in Advance

What I have tried:

webBrowser1.Url = new Uri("some page has table listing values");
           HtmlElement mytable = webBrowser1.Document.GetElementById("I have put class name here (table class name)");

           DataTable dtData = new DataTable();
           dtData.Columns.Add("column1");
           dtData.Columns.Add("column2");
           dtData.Columns.Add("column3");
           dtData.Columns.Add("column4");
           dtData.AcceptChanges();

           DataRow dr = null;

           foreach (HtmlElement row in mytable.GetElementsByTagName("tr"))
           {
               dr = dtData.NewRow();
               HtmlElementCollection cells = row.GetElementsByTagName("td");
               for (int i = 0; i < cells.Count; i++)
               {
                   dr[i] = cells[i].InnerText;
               }
               dtData.Rows.Add(dr);
           }
           dtData.AcceptChanges();

           dataGridView1.DataSource = dtData;
       }
Posted
Updated 18-Sep-18 0:12am

1 solution

I googled "webbrowser control c# get by class name" and this was the first result

c# - How to getelement by class? - Stack Overflow[^]

There are more results if you do a search yourself.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900