I am creating a WPF Page and I want to expose the data resulting from a SQL query on it using DataGrid
. I use C# and SqlDataAdapter
. The relevant code of the query in the code behind file is:
string sqlStr2 = "SELECT Conference_Name, Year FROM ....";
SqlDataAdapter dAdapt2 = new SqlDataAdapter(sqlStr2, cnStr);
DataSet dataSet2 = new DataSet();
dAdapt2.Fill(dataSet2);
The data derived from the query must be inserted into two columns. However, I cannot manage to bind them on the XAML file. Here is the XAML code:
<Grid Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" VerticalAlignment="Top"
HorizontalAlignment="Left">
<DataGrid Name="dtg1" AutoGenerateColumns="False"
RowHeaderWidth="0" ItemsSource="{Binding Path=dataSet2}"
Margin="0,0,0,-23">
<DataGrid.Columns>
<DataGridTextColumn Width="110" Header="Conference"
Binding="{Binding Path=Conference_Name}"/>
<DataGridTextColumn Width="110" Header="Year"
Binding="{Binding Path=Year}"/>
</DataGrid.Columns>
</DataGrid>
The data are not visible when I run the program. What is wrong? Should I declare a source in the header lines of the XAML file?