Category Archives: Visual Basic .Net

Create new DataTable from the DefaultView of the another DataTable

The following article demonstrates how to create new DataTable from the DataView or DefaultView of another DataTable. The general requirement for doing this is that once you have retrieved DataTable from a data source and than you may need to sort or filter the data without retrieving it again. You can use the ToTable() method that creates and returns a new DataTable object based on rows in an existing DefaultView of the DataTable. Continue reading Create new DataTable from the DefaultView of the another DataTable

Export the content of DataGridView to Word Document in vb.net

Several days past i got a comment of my user on the article ‘Create a new Word document using VB.Net‘. He was facing the trouble to export the image to Word document. He wrote his problem as:

I have a Datagridview and all those three fields has been showed in the Datagridview , I have a button Called ” Export to Word”.

There are three columns in the DataGridView, one of them is image type and now we need to export all the content of this Datagridview to the table in the word document. My example contains a DataGridView control named ‘DataGridView1’ that have three columns. Two columns are the TextBox Columns and rest one is of the Image type.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BindGrid()
    End Sub
 
    Private Sub BindGrid()
        DataGridView1.Rows(0).Cells(0).Value = 1
        DataGridView1.Rows(0).Cells(1).Value = "Mohan"
 
        Dim _cellimage As New DataGridViewImageCell
        Try
            DataGridView1.Rows(0).Cells(2).Value = Image.FromFile("D:\Closed.png")
        Catch ex As Exception
 
        End Try
 
    End Sub

There is another button control ‘Export to Document’. On the Click of this button, we will export the content of the datagridview to word document.

    Private Sub btnExport_Click(ByVal sender As System.Object, _
                     ByVal e As System.EventArgs) Handles btnExport.Click
        CreateWordDocument()
    End Sub

The below function will create a word document and export the datagridview.

    Private Sub CreateWordDocument()
        Dim objWord As Word.Application
        Dim objDoc As Word.Document
 
        objWord = CreateObject("Word.Application")
        objWord.Visible = True
        objDoc = objWord.Documents.Add
 
        Dim _RowCount As Integer = DataGridView1.Rows.Count - 1
        Dim _ColCount As Integer = DataGridView1.Columns.Count - 1
 
        Dim ht1 As Word.Table
 
        ht1 = objDoc.Tables.Add(objDoc.Bookmarks.Item("\endofdoc").Range, _
                                _RowCount + 1, _ColCount + 1)
        ht1.Borders.OutsideColor = Word.WdColor.wdColorBlack
        ht1.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
        ht1.Borders.InsideColor = Word.WdColor.wdColorBlack
        ht1.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
 
        For i As Integer = 0 To _RowCount
            ht1.Rows.Add()
            For _col As Integer = 0 To _ColCount
                Dim colType As Type = DataGridView1.Columns(_col).GetType
                If colType.Name = "DataGridViewImageColumn" Then
                    Dim _image As Image = DirectCast(DataGridView1.Rows(i).Cells(_col).Value, Image)
                    Clipboard.SetImage(_image)
                    ht1.Cell(i + 1, _col + 1).Range.Paste()
                Else
                    ht1.Cell(i + 1, _col + 1).Range.Text = _
                    DataGridView1.Rows(i).Cells(_col).Value.ToString()
                End If
            Next
        Next
        objDoc.SaveAs2("C:/tee.docx")
    End Sub

If you get error in the Following line:
Dim _image As Image = DirectCast(DataGridView1.Rows(i).Cells(_col).Value, Image)

Try to replace this line with the following (Suggested by: Xabier Aberasturi)
(Put “FormattedValue” instead of “Value”)
Dim _image As Image = DirectCast(DataGridView1.Rows(i).Cells(_col).FormattedValue, Image)

How to Create System Restore Point from VB.net

System Restore Point is a very useful feature of Windows Operation System. This is basically used when we got some issue with OS because of any new installation. By the help of this feature we can easily restored our OS with any previous date when this point was created (but we will lost all data which we save or installed after restored point date so be careful about it). Continue reading How to Create System Restore Point from VB.net

Render <, >, and & characters in Web Browser in Visual Basic

Some time When you need to use some characters as text like <,> and & in your web page, browsers understand these characters as the part of the html tags and they rendered incorrectly these characters unless characters are escaped as <, >, and & respectively.

This scenario is same in Web browser control in .net. Web browser also rendered these characters incorrectly.
See the following string:

Dim RenderString as String="Welcome to <Authorcode>"

For the correct rendering of above string, we need to encode this string. You can encode this string with the help of System.Web.Server.HtmlEncode() method.

Dim RenderString as String="Welcome to <Authorcode>"
Dim EncodedString As String = Server.HtmlEncode(RenderString )

Server.HtmlEncode() method requires to import the System.Web assembly so add the reference this dll in your project.

How to create your first custom control in C# – Numeric Textbox

Suppose you need to add some additional functionality to your windows control in your application and you have requirement to use this control with same additional functionality many times in the application. This is same as when we use the textbox or label control classes in the Windows form. Continue reading How to create your first custom control in C# – Numeric Textbox