1

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.

3 Answers 3

1

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}  ... />
Sign up to request clarification or add additional context in comments.

Comments

1

Phil is right, but you can also change

 myList.DataContext = ds.Tables[0].DefaultView;

to

 myList.ItemsSource= ds.Tables[0].DefaultView;

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.