Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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.
C#
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

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


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
C#
Clipboard.SetText("String One " & Environment.NewLine & Datatable.Tostring & Environment.NewLine & "String Two", TextDataFormat.Rtf)


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.
Posted
Updated 27-Jan-15 3:49am
v2
Comments
jchoponis 27-Jan-15 21:46pm    
question - does the line

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

"work" before you put in the strings before and after it?

then when you put the two additional strings in the "setText" method, you get the mentioned, undesirable output?

1 solution

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


If you want to copy this html content to the clipboard then you can call this function as
Clipboard.SetText(ConvertDataTableToHtml(DataTable))
 
Share this answer
 
Comments
Muhammad Asim Mughal 28-Jan-15 1:17am    
Thank You Brother !
I will check it and will give the feed back.
Thanks again !

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