Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello everybody
i usea web browers control in my windows form i have html table in web page i lop in that table and get each cell value in need to pass each value to agridview till the first row finished then the second and so on this is my trial .

instead of message pass each string to each cell of grid till the first row finished and then the second row each row has four cells how it can be done ?

What I have tried:

HtmlElement table = webBrowser1.Document.GetElementById("Table1");
try
{
foreach (HtmlElement row in table.GetElementsByTagName("TR"))
{
HtmlElementCollection cells = row.GetElementsByTagName("td");
foreach (HtmlElement cell in cells)
{

String text = cell.InnerText;
if (!String.IsNullOrEmpty(text) && !String.IsNullOrWhiteSpace(text))
{
MessageBox.Show(text);

}
}
}
}
catch (ArgumentOutOfRangeException exc)
{

}
Posted
Updated 6-Sep-16 0:38am
Comments
Maciej Los 5-Sep-16 15:55pm    
Sorry, what's your problem? You mentioned that you want to convert html table into grid view. What you mean?

1 solution

As per your requirement I have written the following code snippet. It may help. Anyways why have you got such requirement?


I have used the following HTML file (testtable.html) :

HTML
<html>
<body>

<table border="1" id="mytable" width="300px">

<tr><td>16</td><td>17</td><td>18</td><td>19</td></tr>
<tr><td>32</td><td>34</td><td>36</td><td>38</td></tr>
<tr><td>48</td><td>51</td><td>54</td><td>57</td></tr>
<tr><td>64</td><td>68</td><td>72</td><td>76</td></tr>
<tr><td>80</td><td>85</td><td>90</td><td>95</td></tr>
</table>

</body>
</html>



PART 1 : Loading the HTML file in WebBrowser Control :

Method 1: by hosting it in any webserver :

C#
webBrowser1.Url = new Uri("http://10.10.10.10/testtable.html");


Method 2: by loading from a local html file :

C#
FileStream fs= File.Open(@"D:\Programming Bench\WindowsFormsApplication1\WindowsFormsApplication1\testtable.html",FileMode.Open);
webBrowser1.DocumentStream = fs;



PART 2 : Get data from WebBrowser Control and load to DataGridView

C#
private void btnGetDataFromWebBrowserCtrl_Click(object sender, EventArgs e)
       {
           HtmlElement mytable = webBrowser1.Document.GetElementById("mytable");

           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;

       }


Hope this helps !!
 
Share this answer
 

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



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