Reputation: 1183
I am trying to populate a listview from sqlite database. My code is
using (SQLiteConnection connection = new SQLiteConnection(@"Data Source=c:\MyProjects\SqliteTest\TestData.db"))
{
connection.Open();
SQLiteDataAdapter ad = new SQLiteDataAdapter();
SQLiteCommand cmd = new SQLiteCommand();
String str = "SELECT Name,Email FROM tblInfo";
cmd.CommandText = str;
ad.SelectCommand = cmd;
cmd.Connection = connection;
DataSet ds = new DataSet();
ad.Fill(ds);
myList.DataContext = ds.Tables[0].DefaultView;
connection.Close();
}
and the xaml code is like
<Grid>
<ListView x:Name="myList"
Height="100"
HorizontalAlignment="Left"
Margin="10,10,0,0"
VerticalAlignment="Top"
Width="300">
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="Name" DisplayMemberBinding="{Binding Path=Name}"></GridViewColumn>
<GridViewColumn Width="100" Header="Email" DisplayMemberBinding="{Binding Path=Email}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
There is no error but list is empty.
Upvotes: 1
Views: 1991
Reputation: 22445
Phil is right, but you can also change
myList.DataContext = ds.Tables[0].DefaultView;
to
myList.ItemsSource= ds.Tables[0].DefaultView;
Upvotes: 1
Reputation: 42991
You're setting the DataContext of the control to your DataSet. You either need to set the ListView's ItemSource to the DataSet, or bind the ItemSource to the DataContext, e.g.
<ListView ItemsSource={Binding} ... />
Upvotes: 1
Reputation: 172
Dataset does not have interfaces INotifyCollectionChanged and INotifyPropertyChanged. so the UI does not know that the dataset has been populated.
If the dataset is changed once, you could bind (initialize) only after dataset is populated.
else, you could create a collection and implement the interfaces.
Upvotes: -1