0

I'm trying to display a column of data from a database in WPF through a listview. Here's my code:

private void OpenExistingBtn_Click(object sender, RoutedEventArgs e)
{
    OpenOrNew.Visibility = System.Windows.Visibility.Collapsed;
    OpenExisting.Visibility = System.Windows.Visibility.Visible;
    con.Open();
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "select docName from [table]";
    cmd.ExecuteNonQuery();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    dataTable.DataContext = dt.DefaultView;
    con.Close();
}

My XAML:

<Grid x:Name="OpenExisting" Visibility="Collapsed">
    <Grid Background="Black" Opacity="0.5">
        <Border MinWidth="250" Background="{x:Null}" BorderThickness="0" CornerRadius="4" HorizontalAlignment="Center" VerticalAlignment="Center">
            <ListView x:Name="dataTable">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Select a file" DisplayMemberBinding="{Binding Path=docName}"></GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
        </Border>
    </Grid>
</Grid>

There is definitely data in my database, and there are no errors when running the code. This is what my database looks like:

This is what my database looks like

1 Answer 1

1

You have to set your ListView.ItemsSource property to dataTable.DefaultView. Example:

OpenExisting.ItemsSource = dt.DefaultView

Sign up to request clarification or add additional context in comments.

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.