DatGrid Problem

P

Prasun

Hello:

I am trying to display an Excel file in a DataGrid. I have Included my code.
It runs through with no exception, but nothing appears in the datagrid


Code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim XLConn As New OleDbConnection
XLConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Test_001.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

Dim XLCmd As New OleDbCommand
XLCmd.Connection = XLConn


Try
XLConn.Open()
Dim XLDA As New OleDbDataAdapter
Dim XLDS As New DataSet("MainDS")
Dim XLTableMain As DataTable = XLDS.Tables.Add("TableMainO")
XLCmd.CommandText = "SELECT * FROM [Sheet1$]"
XLDA.SelectCommand = XLCmd
XLDA.Fill(XLDS, "TableMainO")

Dim XLDV As New DataView
Dim XLDataGrid As New DataGrid

XLDV = New DataView(XLDS.Tables("TableMainO"))
XLDataGrid.DataSource = XLDS
XLDataGrid.DataMember = XLDS.Tables("TableMainO").ToString
XLDataGrid.SetDataBinding(XLDS, "TableMainO")




Finally
XLConn.Close()
End Try
End Sub




Thank You
Prasun
 
N

Norman Yuan

See comment inline.


Prasun said:
Hello:

I am trying to display an Excel file in a DataGrid. I have Included my code.
It runs through with no exception, but nothing appears in the datagrid


Code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim XLConn As New OleDbConnection
XLConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Test_001.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

Dim XLCmd As New OleDbCommand
XLCmd.Connection = XLConn


Try
XLConn.Open()
Dim XLDA As New OleDbDataAdapter
Dim XLDS As New DataSet("MainDS")
Dim XLTableMain As DataTable = XLDS.Tables.Add("TableMainO")
XLCmd.CommandText = "SELECT * FROM [Sheet1$]"
XLDA.SelectCommand = XLCmd
XLDA.Fill(XLDS, "TableMainO")

Add debug code here to make sure there is data in the the datatable:


MessageBox.Show(XLDS.Tables("TableMainO").Rows.Count.ToString)

If Rows.Count>0, Then
You know the data is loaded into DataSet from Excel sheet, it must be
something wrong with data binding
End If
Dim XLDV As New DataView
Dim XLDataGrid As New DataGrid

XLDV = New DataView(XLDS.Tables("TableMainO"))
XLDataGrid.DataSource = XLDS
XLDataGrid.DataMember = XLDS.Tables("TableMainO").ToString

DataTable.ToString() returns "DataTable", not the DtaTable's TableName
property. So, you specified a non-existent table name, hence no data is
bound to the grid. Also, if you use DataGrid.SetDataBinding() method, you do
not need set DataSource and DataMember properties. Try to remove two lines
of code above.
 
P

Prasun

Hi Norman

Thank You for helping out. I figured out how to make it work. I've added
the code below

With Me.XLDataGrid

..SetDataBinding(XLDS, "TableMainO")

..Refresh()

End With



Norman Yuan said:
See comment inline.


Prasun said:
Hello:

I am trying to display an Excel file in a DataGrid. I have Included my code.
It runs through with no exception, but nothing appears in the datagrid


Code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim XLConn As New OleDbConnection
XLConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Test_001.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

Dim XLCmd As New OleDbCommand
XLCmd.Connection = XLConn


Try
XLConn.Open()
Dim XLDA As New OleDbDataAdapter
Dim XLDS As New DataSet("MainDS")
Dim XLTableMain As DataTable = XLDS.Tables.Add("TableMainO")
XLCmd.CommandText = "SELECT * FROM [Sheet1$]"
XLDA.SelectCommand = XLCmd
XLDA.Fill(XLDS, "TableMainO")

Add debug code here to make sure there is data in the the datatable:


MessageBox.Show(XLDS.Tables("TableMainO").Rows.Count.ToString)

If Rows.Count>0, Then
You know the data is loaded into DataSet from Excel sheet, it must be
something wrong with data binding
End If
Dim XLDV As New DataView
Dim XLDataGrid As New DataGrid

XLDV = New DataView(XLDS.Tables("TableMainO"))
XLDataGrid.DataSource = XLDS
XLDataGrid.DataMember = XLDS.Tables("TableMainO").ToString

DataTable.ToString() returns "DataTable", not the DtaTable's TableName
property. So, you specified a non-existent table name, hence no data is
bound to the grid. Also, if you use DataGrid.SetDataBinding() method, you
do
not need set DataSource and DataMember properties. Try to remove two lines
of code above.
XLDataGrid.SetDataBinding(XLDS, "TableMainO")




Finally
XLConn.Close()
End Try
End Sub




Thank You
Prasun
 
Top