How to bind SQL CE Database to DataGrid control?
I have a SQL CE Database that I want to display its data on a DataGrid. I could do a SQL query then do a loop of Items.Add to the DataGrid. But I wonder is it possible to bind the SQL CE database to DataGrid directly without refreshing through a loop or using DataReader.
For instance, if there are new rows added to the Database, the DataGrid will display it automatically without me manually refreshing it (eg. a Button with SQL query).
I tried along the below approach but it didn't work out:
dataGrid1.ItemsSource = connString.Database;
Any suggestion? (BTW开发者_高级运维, I am creating all my Controls in run-time, so no XAML solution)
Rows are not in a database but in a table. A database can have more then one table. So binding the database will not work. I also think you have to get the data out of the table before you can bind them. I am no database expert but I never saw direct binding with a database table.
You need to bind a grid to a specific table.
What you do is fill a data set thru a data adapter and then bind the grid.
cmd = New SqlCeCommand("select * from testtable", conn)
If conn.State = ConnectionState.Closed Then conn.Open()
datAdapter = New SqlCeDataAdapter(cmd)
datSet = New DataSet
datAdapter.Fill(datSet, "myTable")
myBind = New BindingSource(datSet, "myTable")
Me.grid.DataSource = myBind
精彩评论