DataTable和简单字符串的连接 [英] Concatenation of DataTable and a Simple String

查看:101
本文介绍了DataTable和简单字符串的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我的VB.Net项目中有一个不寻常的问题。

要了解我的问题,你必须仔细阅读以下内容。



我正在VB.NET 2010的项目中工作。我在datagridview中有一些数据,我希望使用Outlook格式的表格格式发送这些数据。就像网格一样。 />
我已成功将网格数据转换为数据表并将其粘贴到outlook电子邮件正文中。

代码跟随,其中我将网格数据转换为数据表。 br />

Hello Everyone !
I have an unusual problem in my VB.Net Project.
To Understand my Problem you have to read carefully the following.

I am working in a project of VB.NET 2010. I have some data in a datagridview and i want send this data using outlook in a table format just like a grid.
I have successfully convert the data of grid into a datatable and paste it in outlook email body.
the code is following in which i converted the data of grid to a datatable.

Dim Cols As Integer
Cols = 0
While Cols <= MyGrid.ColumnCount - 1
    If MyGrid.Columns(Cols).Visible = True Then
        DataTable.Columns.Add(MyGrid.Columns(Cols).HeaderText)
    End If
    Cols = Cols + 1
End While

Dim RowNo As Long = 0
While RowNo <= MyGrid.RowCount - 1
    Cols = 0
    DataRow = DataTable.NewRow
    While Cols <= DataTable.Columns.Count - 1
        DataRow(Cols) = MyGrid.Rows(RowNo).Cells(Cols).Value
        Cols = Cols + 1
    End While
    DataTable.Rows.Add(DataRow)
    RowNo = RowNo + 1
End While





数据从网格转换为数据表然后我把它弄干净到剪贴板





the data is converted from grid to datatable and then i coppy it to clipboard

Clipboard.SetText(Datatable.Tostring, TextDataFormat.Rtf)





现在问题是我想插入两个字符串,一个字符串在表格之前,一个字符串在表格之后。



i使用以下代码:



now the problem is that i want to insert two strings also,one string before table and the one string after table.

i use the following code for this

Clipboard.SetText("String One " & Environment.NewLine & Datatable.Tostring & Environment.NewLine & "String Two", TextDataFormat.Rtf)





但是当我将它粘贴在outlook或word页面上时,字符串一和二在表格中被诅咒但是我想看起来如下。





but as i paste it on the outlook or word page then the string one and two are murged inside the table but i want to look like below.

String One<br />
<br />
"DATA TABLE"<br />
<br />
String Two





希望你有我的问题!谁能帮我?我将非常感谢这种行为。



Hopefully you got my problem ! Can anyone help me? I Shall be very grateful for this act of kind.

推荐答案

根据我的知识 Datatable.Tostring 没有任何意义。我的实验表明, Datatable.Tostring 什么都没有。因此,我建议您将 Datatable 转换为 HTML表并将其复制到剪贴板以备将来使用。以下功能将帮助您根据需要执行此操作。它将添加标题文本和页脚文本以及表格。



AS per my knowledge Datatable.Tostring doesn't make any sense. My experiment reveals that Datatable.Tostring gives nothing. So that i suggest you to convert the Datatable to HTML Table and copy it to the clipboard for future use. following function will help you to do this as per your needs. it will add header text and footer text along with the table.

Public Shared Function ConvertDataTableToHtml(ByVal dt As DataTable) As String
       Dim html As New StringBuilder
       html.Append("Start String") ' <--- this is the start string you have to insert as the header
       html.Append("<table>") '<-- building of html table
       'add header row it includes the column names
       html.Append("<tr>")
       For i As Integer = 0 To dt.Columns.Count - 1
           html.Append("<td>" & dt.Columns(i).ColumnName & "</td>")
       Next
       html.Append("</tr>")
       'add rows
       For i As Integer = 0 To dt.Rows.Count - 1
           html.Append("<tr>")
           For j As Integer = 0 To dt.Columns.Count - 1
               html.Append("<td>" + dt.Rows(i)(j).ToString() + "</td>")
           Next
           html.Append("</tr>")
       Next
       html.Append("</table>")
       html.Append("End String") ' <--- this is the End string /Footer text you have to insert as the header
       Return html.ToString
   End Function





如果你想将这个html内容复制到剪贴板然后你可以将此函数称为



If you want to copy this html content to the clipboard then you can call this function as

Clipboard.SetText(ConvertDataTableToHtml(DataTable))


这篇关于DataTable和简单字符串的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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