Reputation: 405
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 ocitajTabelu()
{
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();
}
}
Upvotes: 0
Views: 507
Reputation: 842
select * is a bad practice (what if your tables grow to dozens of columns and you need just 1?). But you could alias the column (select table1.foo foo1, table2.foo foo2...)
That should work because then you could bind to table1.foo1 and table2.foo2 -- they will be different column names.
Upvotes: 1