I cannot get my DataGrid
to populate with data. I'm new to XAML. I'm using an OracleDataReader
object and would like to add it to the DataGrid
. First here is the XAML:
<DataGrid Name="grdResults" FontSize="25" AutoGenerateColumns="False" CanUserResizeRows="False" DockPanel.Dock="Top" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=extName}" Header="Name" Width="*" />
<DataGridTextColumn Binding="{Binding Path=location_note}" Header="Location" Width="*" />
<DataGridTextColumn Binding="{Binding Path=crate_no}" Header="Crate" Width="*"/>
</DataGrid.Columns>
</DataGrid>
And here is the behind the scenes. Seems Like it should work but it is not. The fields are named the same as they are bound in the XAML. RS is a populated OracleDataReader
.
Dim dt As New DataTable
dt.Load(rs)
grdResults.DataContext = dt.DefaultView
Can anyone see what I'm doing wrong?
EDIT :
I was able to get it to work. Probably not the best way but works for me. I took out the datagrid.columns and childs from the XAML and put this in the behind the scenes between dt.Load(rs) And setting the datacontext
grdResults.Columns.Add(New DataGridTextColumn() With { _
.Header = "Name", _
.Binding = New Binding(String.Format("[{0}]", "extName"))})
grdResults.Columns.Add(New DataGridTextColumn() With { _
.Header = "Location", _
.Binding = New Binding(String.Format("[{0}]", "location_note"))})
grdResults.Columns.Add(New DataGridTextColumn() With { _
.Header = "Crate", _
.Binding = New Binding(String.Format("[{0}]", "crate_no"))})