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.
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.