DbDataReader to DataGrid WPF

141 views Asked by At

I have a module which returns DbDataReader objects. This is a module that is tried and trusted and in production use with DataTable/DataGridView in Windows.Forms, so I do not wish to change it.

I tried doing the same with WPF, passing a DataTable to ItemsSource, but that failed because it's expecting an IEnumerator that DataTable does not implement.

I remembered DbDataReader does implement that interface, and it occured to me that ItemsSource may be wanting to read data into an internal buffer, so I tried passing DbDataReader and it worked....until I completed the code with a finally clause!

Then I found that DataTableExtensions does allow me to get an enumerator from a DataTable. but it doesn't seem to be iterating the data!

Note that I DO NOT need to edit the data, and what I got from DbDataReader was fine, all I need is a copy of this data to be available for the grid...how can I do this, it must be simple!

Thanks for any help!

As requested, here is code;

This is basically the type of thing I'm used to doing with Windows Forms:

DbDataReader dr=null;
DataTable dt=null;
try{
    dr = AModule.FetchData(Params....);  
    dt.Load(dr);                
}
finally{
    if(dr!=null)dr.Close();
}
dataGridView1.DataSource=dt;

What's the equivalent with WPF?

1

There are 1 answers

0
Roger Irwin On

I resolved this in the end.

I had found several similar posts that suggested using:

DataTableExtensions.AsEnumerable(DataTable dt);

To get an enumerator interface for the DataTable, but this simply does not work.

But using:

DataTableExtensions.AsDataView(DataTable dt);

Wraps the DataTable with a DataView class, and this is compatible with WPF's DataGrid.