I have a datagrid where I display my data from the itemsource that has joined tables. How can I create a column in a datagrid for fields with the same name? I tried something like Binding="{Binding table1.name}" and Binding="{Binding table2.name}",but it shows blank.
this is the samle of my code:
XAML:
<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" VerticalAlignment="Top" Height="227" Width="1102" Margin="0,202,0,0" ItemsSource="{Binding DataSource}" AutoGenerateColumns="False" IsReadOnly="True"><DataGrid.Columns><DataGridTextColumn Header="Name1" Binding="{Binding table1.name}" Width="300" /><DataGridTextColumn Header="Name2" Binding="{Binding table2.name}" Width="300"/></DataGrid.Columns></DataGrid>
my code behind:
public void readTable() { using (SqlConnection sc = new SqlConnection(ConString)) { sc.Open(); string query = "select * from table1 left join table2 on table1.name_id = table2.id where table.order_id=@id"; SqlCommand com = new SqlCommand(query, sc); com.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(selectedID); using (SqlDataAdapter adapter = new SqlDataAdapter(com)) { DataTable dt = new DataTable(); adapter.Fill(dt); adapter.Update(dt); dataGridPretragaObjekta.ItemsSource = dt.DefaultView; } sc.Close(); } }