从vb.net datagridview生成html表格 [英] Generating html table from vb.net datagridview

查看:170
本文介绍了从vb.net datagridview生成html表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据datagridview中的数据生成HTML表格。 (如果有人想知道,我使用vb.net)



datagridview由5列组成,而行的数量可以根据

我试图在下面生成的表代码的结构如下:

 < tr> 
< td> column 1 row1< / td>
< td>栏2 row1< / td>
< td>栏3 row1< / td>
< td>栏4 row1< / td>
< td>列5 row1< / td>
< / tr>
< tr>
< td>第1列第2行< / td>
< td>第2列第2行< / td>
< td>栏3 row2< / td>
< td>列4 row2< / td>
< td>列5 row2< / td>
< / tr>
< tr>
< td>第1列第3行< / td>
< td>第2栏第3行< / td>
< td>栏3 row3< / td>
< td>栏4 row3< / td>
< td>列5 row3< / td>
< / tr>

ect ....

并且在完整的行被保存之前和之后的标签。下面的代码显示了我试图解决这个问题的尝试,但是我迷失了方向,然后发现它很高兴在论坛上寻求帮助。

注意:(请原谅在代码中出现的错误,这些错误是在调整后才开始工作的)。

  Private Sub Button1_Click(sender As Object,e As EventArgs)Handles Button1.Click 

Dim col1 As String
Dim col2 As String

Dim Count As Integer = 1
For i As Integer = 0到DataGridView1.Rows.Count - 1

如果Count = 5那么

对于每行DataGridViewRow在DataGridView1.Rows中

如果不是row.IsNewRow然后

col1 = row.Cells(0).Value.ToString
col2 = row.Cells(1).Value.ToString

MsgBox(< tr>< td>& col1&& col2&< / td>< / tr>)
Count = 0
End If
Next

ElseIf Not Count = 5 Then

For j As Integer = 0 To DataGridView1.Rows.Count - 1
对于每行2作为DataGridViewRow在DataGridView1.Rows

如果不是row2.IsNewRow然后

col1 = row2.Cells(0).Value.ToString
col2 = row2.Cells(1).Value.ToString

MsgBox(< td>& col1& & col2& amp; < / td>)

结束If
Next
Count = +1
Next
End If
Next

结束


解决方案

我最近使用的代码将DataGridView呈现到HTML表格中用于发送电子邮件。它使用C#,但翻译起来应该不会太困难。它应该是非常简单的,你添加HTML元素,自动将HTML应用到数据。 $ b

 使用System.Web.UI; 
使用Web = System.Web.UI.WebControls;

...

私有字符串DataGridViewToHTML()
{
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);

Web.Label introBody = new Web.Label();
introBody.Text =;
introBody.ID =bodyText;
introBody.RenderControl(hw);

hw.WriteBreak();
hw.WriteBreak();

//数据表。
Web.Table tbl = new Web.Table();
tbl.GridLines = Web.GridLines.Both;
Web.TableHeaderRow thr = new Web.TableHeaderRow();
//添加列标题
foreach(DataGridViewColumn col in dgvRcds.Columns)
{
Web.TableHeaderCell thc = new Web.TableHeaderCell();
thc.Text = col.HeaderText;
thr.Controls.Add(thc);
}
tbl.Controls.Add(thr);
//添加每行
foreach(DataGridViewRow在dgvRcds.Rows中的行)
{
Web.TableRow tr = new Web.TableRow();
foreach(DataGridViewCell val in row.Cells)
{
Web.TableCell tc = new Web.TableCell();
tc.Text = val.EditedFormattedValue.ToString();
tr.Controls.Add(tc);
}
tbl.Controls.Add(tr);
}
//在html
中渲染表tbl.RenderControl(hw);

//返回原始html字符串。
return sw.ToString();
}


I need to generate a HTML table based upon data found in my datagridview. (I use vb.net if anyone wants to know)

The datagridview consists out of 5 columns whilst the amount of rows are pendable on how many actions have been recorded by the end user.

The structure of the table code I am trying to generate in as follows:

    <tr>
<td>column 1 row1</td>
<td>column 2 row1</td>
<td>column 3 row1</td>
<td>column 4 row1</td>
<td>column 5 row1</td>
</tr>
    <tr>
<td>column 1 row2</td>
<td>column 2 row2</td>
<td>column 3 row2</td>
<td>column 4 row2</td>
<td>column 5 row2</td>
</tr>
    <tr>
<td>column 1 row3</td>
<td>column 2 row3</td>
<td>column 3 row3</td>
<td>column 4 row3</td>
<td>column 5 row3</td>
</tr>

ect....

The trouble Im having though is placing the and tags after and before a complete row has been saved. the following code shows my attempt at trying to resolve this problem but i have lost my way and then found it high time to ask around on the forum for help

NOTE:(please excuse the errors in the code which came about after tweaking around trying to get things to work).

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim col1 As String
    Dim col2 As String

    Dim Count As Integer = 1
    For i As Integer = 0 To DataGridView1.Rows.Count - 1

        If Count = 5 Then

            For Each row As DataGridViewRow In DataGridView1.Rows

                If Not row.IsNewRow Then

                    col1 = row.Cells(0).Value.ToString
                    col2 = row.Cells(1).Value.ToString

                    MsgBox("<tr><td>" & col1 & " " & col2 &"</td></tr>")
                    Count = 0
                End If
            Next

        ElseIf Not Count = 5 Then

            For j As Integer = 0 To DataGridView1.Rows.Count - 1
                For Each row2 As DataGridViewRow In DataGridView1.Rows

                    If Not row2.IsNewRow Then

                        col1 = row2.Cells(0).Value.ToString
                        col2 = row2.Cells(1).Value.ToString

                        MsgBox("<td>" & col1 & " " & col2 & "</td>")

                    End If
                Next
                Count = +1
            Next
        End If
    Next

    End

解决方案

Here's an example of some code I used recently that renders a DataGridView into an HTML table for emailing. It is in C#, but it shouldn't be too difficult for you to translate. It should be pretty straightforward, you're adding HTML elements that apply HTML to the data automatically.

 using System.Web.UI;
 using Web = System.Web.UI.WebControls;

// ...

 private string DataGridViewToHTML()
    {
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        Web.Label introBody = new Web.Label();
        introBody.Text = "";
        introBody.ID = "bodyText";
        introBody.RenderControl(hw);

        hw.WriteBreak();
        hw.WriteBreak();

        //Table for data.
        Web.Table tbl = new Web.Table();
        tbl.GridLines = Web.GridLines.Both;
        Web.TableHeaderRow thr = new Web.TableHeaderRow();
        // add column headers
        foreach(DataGridViewColumn col in dgvRcds.Columns)
        {
            Web.TableHeaderCell thc = new Web.TableHeaderCell();
            thc.Text = col.HeaderText;
            thr.Controls.Add(thc);
        }
        tbl.Controls.Add(thr);
        //add each row
        foreach(DataGridViewRow row in dgvRcds.Rows)
        {
            Web.TableRow tr = new Web.TableRow();
            foreach(DataGridViewCell val in row.Cells)
            {
                Web.TableCell tc = new Web.TableCell();
                tc.Text = val.EditedFormattedValue.ToString();
                tr.Controls.Add(tc);
            }
            tbl.Controls.Add(tr);
        }
        //render table in html
        tbl.RenderControl(hw);

        //return raw html string.
        return sw.ToString();
    }

这篇关于从vb.net datagridview生成html表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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