将HTML表传递给网格视图C# [英] Pass HTML table to grid view C#

查看:78
本文介绍了将HTML表传递给网格视图C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好每个人

i usea web浏览器在我的windows窗体中控制我在网页中有html表我在该表中删除并获取需要的每个单元格值将每个值传递给agridview直到第一行完成然后第二个等等这是我的试用。



而不是消息将每个字符串传递给网格的每个单元格,直到第一行完成,然后每行第二行row有四个单元格怎么做?



我尝试了什么:



HtmlElement table = webBrowser1.Document.GetElementById(Table1);

try

{

foreach(表中的HtmlElement行) .GetElementsByTagName(TR))

{

HtmlElementCollection cells = row.GetElementsByTagName(td);

foreach(HtmlElement cell in细胞)

{



String text = cell.InnerText;

if(!String.IsNullOrEmpty(text)&& !String.IsNullOrWhiteSpace(text))

{

MessageBox.Show(text);



} < br $>
}

}

}

catch(ArgumentOutOfRangeException exc)

{



}

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)
{

}

推荐答案

根据您的要求,我编写了以下代码片段。它可能有所帮助。无论如何为什么你有这样的要求?





我使用了以下HTML文件(testtable.html):



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







第1部分:在WebBrowser控件中加载HTML文件:



方法1:通过在任何网络服务器中托管它:






PART 1 : Loading the HTML file in WebBrowser Control :

Method 1: by hosting it in any webserver :

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





方法2:从本地html文件加载:





Method 2: by loading from a local html file :

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







第2部分:从中获取数据WebBrowser控制并加载到DataGridView






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

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


这篇关于将HTML表传递给网格视图C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆