Problem with Datagrid

SonicBoomAu

Well-known member
Joined
Oct 30, 2003
Messages
179
Location
Australia
Hi All,

I am having problem retrieving information from an Access Database and putting that information into a DataGrid.

Here is my Code:

[VB]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Set strDatabaseName "C:\Database\database.mdb"
strDatabaseName = "Database Location"

Dim cnAdoNetConnection As New OleDb.OleDbConnection
Dim daDataAdapter As New OleDb.OleDbDataAdapter
Dim ds As New DataSet

Try

Open the connection to the database
cnAdoNetConnection.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
strDatabaseName
cnAdoNetConnection.Open()

Retrieve all the Asset Information
daDataAdapter = New OleDb.OleDbDataAdapter("SELECT * from DeletedAsset", _
cnAdoNetConnection)

daDataAdapter.Fill(ds)
DataGrid1.DataSource = ds.Tables

Close the connection to the database
cnAdoNetConnection.Close()

Catch ex As Exception
MessageBox.Show("An error has occurred! Error: " & ex.Message, _
"Title", MessageBoxButtons.OK, MessageBoxIcon.Error)

Close the connection to the database
cnAdoNetConnection.Close()
End Try

End Sub
[/VB]

Here is the Error I am receiving:

"Complex DataBinding accepts as a data source either an IList or an IListSource."

It throws the error up at the "DataGrid1.DataSource = ds.Tables" line.

If anyone knows of a good datagrid tutorial or example, could they please let me know.

Any help is greatly appreciated. :)
 
A bit of sample code i have in a sample application ive got: The problem your having seems to be where your not setting the datasource to an actual table, but this will give you a pattern to go by for future reference:

Code:
Dim objDataCommand As New OleDbCommand
Dim strConnectionString As String
Dim strSQL As String = "SELECT * FROM Customers"

Connect to an Access database - using the sample Northwind database
strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Visual Studio Projects\SourceSeek\bin\res\Northwind.mdb;User Id=admin;Password=;"

Create a dataadapter and dataset, and fill the dataset with our query
Dim objDataAdapter As New OleDbDataAdapter(strSQL, strConnectionString)
Dim objDataset As New DataSet

objDataAdapter.Fill(objDataset, "Customers")

Simply bind the data to our dataset and set the displayed table
to the Customers datatable
Me.DataGrid1.DataSource = objDataset
Me.DataGrid1.DataMember = "Customers"
 
stustarz said:
A bit of sample code i have in a sample application ive got: The problem your having seems to be where your not setting the datasource to an actual table, but this will give you a pattern to go by for future reference:
Code:
Me.DataGrid1.DataMember = "Customers"

Thanks for that. Once I set the dataMember to "DeletedAsset" it all came together.

You dont know of any tutorial web site on Datagrids by any chance???

Once again thanks for your help.
 
Back
Top