Click here to Skip to main content
15,897,226 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: stackhash _2f16 Pin
Dave Kreskowiak9-Jun-13 5:18
mveDave Kreskowiak9-Jun-13 5:18 
GeneralRe: stackhash _2f16 Pin
champagne_charly9-Jun-13 5:45
champagne_charly9-Jun-13 5:45 
GeneralRe: stackhash _2f16 Pin
Dave Kreskowiak9-Jun-13 6:17
mveDave Kreskowiak9-Jun-13 6:17 
QuestionReplication status Pin
alaa_hossain8-Jun-13 4:09
alaa_hossain8-Jun-13 4:09 
AnswerRe: Replication status Pin
Abhinav S8-Jun-13 4:36
Abhinav S8-Jun-13 4:36 
Questionnew language come in my code Pin
foad aldhoh7-Jun-13 4:47
foad aldhoh7-Jun-13 4:47 
AnswerRe: new language come in my code Pin
TnTinMn7-Jun-13 10:48
TnTinMn7-Jun-13 10:48 
Questiondatatable -> wpf datagrid -> new datatable Pin
Member 98926065-Jun-13 6:35
Member 98926065-Jun-13 6:35 
Hi Guys,

I have a quick and hopefully not to difficult question to the commmunity. I have a program that reads an excel spreadsheet, adds the data to a datatable inside a dataset and displays it in a wpf datagrid using databinding.

Here is the code for it, it works fine just like I imagined it Smile | :) . I also have a funciton that returns all the sheets in a excel spreadsheet.

VB
Private Sub Button_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim m_sConn1 As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                  "Data Source=C:\Temp\Test.xlsx;" & _
                  "Extended Properties=""Excel 12.0 Xml;HDR=No;IMEX=1"""     'IMEX=1 would treat all columns as text

        Dim conn1 As New System.Data.OleDb.OleDbConnection(m_sConn1)
        conn1.Open()
        Dim da As OleDbDataAdapter
        Dim ds As New System.Data.DataSet
        Dim test As String
        test = "[Sheet1b$]"
        Dim teststring As String
        teststring = "Select * FROM" & test & """"

        da = New OleDbDataAdapter(teststring, conn1)
        da.Fill(ds, "TestTable")
        da.Dispose()

        Dim bindingSource As New BindingSource(ds, "TestTable")

        excelDataGrid.ItemsSource = bindingSource
        conn1.Close()

    End Sub

    Private Sub getList_Click(sender As Object, e As RoutedEventArgs) Handles getList.Click

        Dim m_sConn1 As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                  "Data Source=C:\Temp\Test.xlsx;" & _
                  "Extended Properties=""Excel 12.0 Xml;HDR=No;IMEX=1"""     'IMEX=1 would treat all columns as text
        Dim conn1 As New System.Data.OleDb.OleDbConnection(m_sConn1)
        conn1.Open()
        Dim test As New System.Data.DataTable
        test = conn1.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
        If test Is Nothing OrElse test.Rows.Count < 1 Then
            Throw New Exception("Error: Could not determine the name of the first worksheet.")
        End If
        Dim firstSheetName As String
        Dim i As Integer

        For i = 0 To test.Rows.Count - 1
            firstSheetName = test.Rows(i)("TABLE_NAME").ToString()
            List1.Items.Add(firstSheetName)

        Next



    End Sub


My question now is this. I'd like to have the program iterate through the first column of the data and match the entries with known parameters. For example. the user has a cell that says EmpID, I want the program to recognize it and change it to Employee ID. Upon recognizing all of these, I want the program to display the data with the new cell values in a different datagrid.

My thought was this: Load data from excel -> fill datatable in dataset -> bind data to datagrid for visual confirmation that data was imprted correctly -> in different sub obtain the databinding for the wpf datagrid and fill a new datatable in a new dataset -> go through the recognizing function -> and spit out the data in a new datatable

I tried looking for a way to do it but am having trouble with getting the data. All I could find online was how to get data into a datagrid and not the other way around.

initially I thought it would be as simple as

VB
Dim TestDataTabe As System.Data.DataTable
        TestDataTabe = DirectCast(excelDataGrid.ItemsSource, System.Data.DataView).ToTable


but that didn't seem to work and it told me an error: Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataView'.

so now I am stuck and can't find anything on Dr. Google about getting the datatable that is used to fill the datagrid. Would someone be able to help me out, it would be greatly appreciated. Is it possible to have a databinding for a datatable? As in Datatable.bindingsource = datagrid.bindingsource?

Update:
so it seems that I always sorta solve my problems when I post on a forum lol. So here is what I did and hope that you guys can give me input to see if it is a viable way to do this sort of thing.

Instead of creating a dataset I created a datatable and instead of using binding source I used datagrid.itemssource = datatable.defaultview

then i direct casted it totable here is the code

VB
Private Sub Button_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim m_sConn1 As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                  "Data Source=C:\Temp\Test.xlsx;" & _
                  "Extended Properties=""Excel 12.0 Xml;HDR=No;IMEX=1"""     'IMEX=1 would treat all columns as text

        Dim conn1 As New System.Data.OleDb.OleDbConnection(m_sConn1)
        conn1.Open()
        Dim da As OleDbDataAdapter
        'Dim ds As New System.Data.DataSet
        Dim ds As New System.Data.DataTable
        Dim test As String
        test = "[Sheet1b$]"
        Dim teststring As String
        teststring = "Select * FROM" & test & """"

        da = New OleDbDataAdapter(teststring, conn1)
        'da.Fill(ds, "TestTable")
        da.Fill(ds)
        da.Dispose()

        'Dim bindingSource As New BindingSource(ds, "TestTable")

        ' excelDataGrid.ItemsSource = bindingSource
        excelDataGrid.ItemsSource = ds.DefaultView


        'Public DataAdapter As New sqlClient.sqlDataAdapter(sqlCommandstr, sqlConnectStr)

        'Public dbTable As New DataTable

        'First I define the sqlAdapter and a data table object, now,

        'DataAdapter.SelectCommand.CommandText="select * from Company" --> where Company is just a table with few fields

        'DataAdapter.fill(dbTable)
        'DataGrid1.ItemSource=dbTable.DefaultView
     


        conn1.Close()

    End Sub

Private Sub Button_Click_2(sender As Object, e As RoutedEventArgs)
        Dim TestDataTabe As System.Data.DataTable
        TestDataTabe = DirectCast(excelDataGrid.ItemsSource, System.Data.DataView).ToTable


        'listItem.Items.Add(TestDataTabe.Rows(3).Item(1))

    End Sub


Now I got more questions lol How would you set the itemssource of the datagrid to a table inside a dataset? Sorry for the long post but maybe it helps someone out.
QuestionConvert VB6 project to VB.Net Pin
bhagyashree apte5-Jun-13 2:57
bhagyashree apte5-Jun-13 2:57 
AnswerRe: Convert VB6 project to VB.Net Pin
Abhinav S5-Jun-13 16:47
Abhinav S5-Jun-13 16:47 
QuestionHow to send Email using VB 6.0 Pin
Saranya-from-Tamil-Nadu-India4-Jun-13 23:28
Saranya-from-Tamil-Nadu-India4-Jun-13 23:28 
AnswerRe: How to send Email using VB 6.0 Pin
Richard MacCutchan4-Jun-13 23:34
mveRichard MacCutchan4-Jun-13 23:34 
QuestionSmart Card and CredUIPromptForWindowsCredentials Pin
dfaulhaber2k14-Jun-13 11:28
dfaulhaber2k14-Jun-13 11:28 
QuestionFindFirstFileEx() and Unicode Pin
treddie4-Jun-13 10:03
treddie4-Jun-13 10:03 
AnswerRe: FindFirstFileEx() and Unicode Pin
Dave Kreskowiak4-Jun-13 10:39
mveDave Kreskowiak4-Jun-13 10:39 
GeneralRe: FindFirstFileEx() and Unicode Pin
treddie4-Jun-13 11:04
treddie4-Jun-13 11:04 
GeneralRe: FindFirstFileEx() and Unicode Pin
Dave Kreskowiak4-Jun-13 11:12
mveDave Kreskowiak4-Jun-13 11:12 
GeneralRe: FindFirstFileEx() and Unicode Pin
treddie4-Jun-13 11:32
treddie4-Jun-13 11:32 
GeneralRe: FindFirstFileEx() and Unicode Pin
treddie4-Jun-13 12:08
treddie4-Jun-13 12:08 
GeneralRe: FindFirstFileEx() and Unicode Pin
Garth J Lancaster4-Jun-13 12:41
professionalGarth J Lancaster4-Jun-13 12:41 
GeneralRe: FindFirstFileEx() and Unicode Pin
treddie4-Jun-13 16:25
treddie4-Jun-13 16:25 
GeneralRe: FindFirstFileEx() and Unicode Pin
Dave Kreskowiak4-Jun-13 12:54
mveDave Kreskowiak4-Jun-13 12:54 
GeneralRe: FindFirstFileEx() and Unicode Pin
treddie4-Jun-13 16:47
treddie4-Jun-13 16:47 
GeneralRe: FindFirstFileEx() and Unicode Pin
Dave Kreskowiak4-Jun-13 17:37
mveDave Kreskowiak4-Jun-13 17:37 
GeneralRe: FindFirstFileEx() and Unicode Pin
treddie4-Jun-13 19:17
treddie4-Jun-13 19:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.