Set source of Datagrid with query

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

Does anyone know how to set the source of a dtatagrid by means of a query and that the datagrid only shows the columns from the query. I have some code like this

Me.OleDbSelectCommand1.CommandText = "SELECT X, Y " &
"FROM TABLE " &
"WHERE TABLE.[X] = 2
Me.OleDbSelectCommand1.Connection = Me.OleDbConnection
Me.Dataset1.Clear(
Me.OleDbDataAdapter1.Fill(Dataset1
Me.DataGrid1.SetDataBinding(Dataset, "TABLE"

Now the problem is that the datagrid shows all columns of TABLE instead of just the columns X and Y. I'm getting the columns of the complete table. Is it possible to show only the columns X,Y

Thanx...
 
Hi Maurice,

Strange.

Me.OleDbSelectCommand1.CommandText = "SELECT X, Y " & _
"FROM TABLE " & _
"WHERE TABLE.[X] = 2 "
Me.OleDbSelectCommand1.Connection = Me.OleDbConnection1

me.oledbdatadapter = new oledbdataadapter("selectstring", oledbconnection1)
(above is not the reason of the error but I show it because it is easier)
Me.Dataset1.Clear()
Me.OleDbDataAdapter1.Fill(Dataset1)
Me.DataGrid1.SetDataBinding(Dataset, "TABLE")
I asume this is a typo and I would take the row beneath instead of the row
above.
me.datagrid1.datasource=Dataset1.tables("TABLE")

Can you try this and tell if it did give the result you wanted?

Cor
 
Cor, thx for help

I've found another way to solve it

strQry = "SELECT X, Y " &
"FROM TABLE " &
"WHERE TABLE.[X] = 2
Dim adaptOLEDB As New OleDb.OleDbDataAdapter(strQry, OleDbConnection1
Dim datRS As DataSet = New DataSe
adaptOLEDB.Fill(datRS, "TABLE"
Me.DataGrid1.SetDataBinding(datRS, "TABLE"

this works!
 
Hi Maurice,

Than that typo was not a typo I asume, because that is what you actualy did
change in my opinion.

Cor
 
Back
Top