Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

May i ask how to load selected columns data in datagridview

$
0
0

I am working in Visual Studio 2010 and have two forms in my Windows Application.

In the first one (Form 1.vb), the user read excel file to datagridview1. user selected which columns user want and load selected columns data in datagridview2. Datagridview 2 is under form2

can someone guide me how to write the code i am using vb.net language

this is my read excel file code

        Dim _filename As String = OpenFile()

        Dim _conn As String

        _conn = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & _filename & ";" & "Extended Properties=Excel 8.0;"

        Dim _connection As OleDbConnection = New OleDbConnection(_conn)

        Dim da As OleDbDataAdapter = New OleDbDataAdapter()

        Dim _command As OleDbCommand = New OleDbCommand()

        _command.Connection = _connection

        _command.CommandText = "SELECT * FROM [Sheet1$]"

        da.SelectCommand = _command

        Try

            da.Fill(ds1, "sheet1")

            MessageBox.Show("Read Successful")

            Me.DataGridView1.DataSource = ds1

            Me.DataGridView1.DataMember = "sheet1"

        Catch e1 As Exception

            MessageBox.Show("Read Fail, correct Column name in the sheet!")

        End Try

 Private Function OpenFile() As String
        'build and configure an OpenFileDialog

        Dim OFD As New OpenFileDialog
        With OFD
            .AddExtension = True
            .CheckFileExists = True
            .Filter = "Excel  files Excel files (*.xls)|*.xls"
            .Multiselect = False
            .Title = "Select an workbook to open"
        End With

        'show the ofd and if a file was selected return it, otherwise return nothing
        If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
            Return OFD.FileName
        Else
            Return Nothing
        End If
    End Function

after read excel data it will show to datagridview1

i have develop a table display table

below is my code 

 Public Sub display()
        Try
            Dim dt2 As DataTable = New DataTable()
            dt2.Columns.Add("Id", Type.[GetType]("System.String"))
            dt2.Columns.Add("Sheet1", Type.[GetType]("System.String"))
            dt2.Columns.Add("F1", Type.[GetType]("System.String"))
            dt2.Columns.Add("F2", Type.[GetType]("System.String"))
            dt2.Columns.Add("F3", Type.[GetType]("System.String"))
            dt2.Columns.Add("F4", Type.[GetType]("System.String"))
            dt2.Columns.Add("F5", Type.[GetType]("System.String"))
            dt2.Columns.Add("F6", Type.[GetType]("System.String"))
            dt2.Columns.Add("F7", Type.[GetType]("System.String"))
            dt2.Columns.Add("F8", Type.[GetType]("System.String"))
            dt2.Columns.Add("F9", Type.[GetType]("System.String"))
            dt2.Columns.Add("F10", Type.[GetType]("System.String"))
        Catch e As Exception
            MessageBox.Show(e.Message)
        End Try
    End Sub

thanks a lot.


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>