I have a button that when clicked will run a stored procedure on a SQL Server and display the resulting data in a grid within the same window.
In windows forms world, I’d create a datatable, use a dataadapter to fill it and then assign the datatable to the DataSource propert of my DataGridView and poof… there’s my data.
I’m tried something similar in WPF using a ListView with a Gridview and i cant seem to make it work. I have the following in my XAML:
<ListView Grid.Row="1" Name="Preview" ItemsSource="{Binding Path=Report}">
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=FormalName}" />
</GridView>
</ListView>
And in my C# code
private void CreateReport(object sender, RoutedEventArgs e)
{
DataTable dt = new DataTable("Report");
SqlConnection cn = new SqlConnection("Data Source=DailyTimesheets;
Initial Catalog=DailyTimesheets;Integrated Security=SSPI");
SqlCommand cm = new SqlCommand("Reports.PayrollHoursInterface", cn);
cm.Parameters.AddWithValue("@PayBatchID", 722);
cm.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cm);
da.Fill(dt);
Preview.DataContext=dt;
}
When I clicka the button (that fires the CreateReport Method), my datatable gets filled and assigned to the Datacontext, but nothing displays.
I made a sample app and discovered you need to surround your GridView with ListView.View and set your ItemsSource to {Binding} just like the following: