0

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"))})
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Loogawa
  • 389
  • 6
  • 24
  • I fully discourage the use of `System.Data` classes in WPF client-side. Create a proper data model to hold your data and proper ViewModels to define the application logic. – Federico Berasategui Apr 26 '13 at 15:08
  • But if I had that would it be possible to display it in a grid? – Loogawa Apr 26 '13 at 15:10
  • of course dude. WPF is much more capable than winforms. I don't understand your question. – Federico Berasategui Apr 26 '13 at 15:18
  • Did you try it with AutoGenerateColumns = True? – PatFromCanada Apr 26 '13 at 15:26
  • Yes but there are some rows that I do not want to use. – Loogawa Apr 26 '13 at 15:45
  • You need to use some sort of an ObservableCollection or a collection that raises NotifyOnPropertyChange. I strongly recommend going to a ModelView/View/Model for better isolation and binding simplicity. – FHnainia Apr 26 '13 at 15:46
  • Its such a headache for me trying to wrap my head around the MVVM stuff. And my company has been doing winforms so long it just seems hard to adapt it. Especially without any formal training and just reading on the internet. – Loogawa Apr 26 '13 at 15:50

1 Answers1

2

Actually, you did right for AutoGeneratedColumn = false If you want to skip some columns of the database, you can just firstly hide all of them by setting Columns.Item(x).Visibility = Windows.Visibility.Hidden and then show only selected ones.

To skip rows you can just remove them from the DataView (it is better to create separate DataView for viewing, if you want to do that, and provide custom data collection method)

Another (better) way of disabling separate row is described here

Community
  • 1
  • 1
George
  • 692
  • 5
  • 10