简体   繁体   中英

binding the WPF data grid from SqlDataReader object

How to bind the data in SqlDataReader object into WPF data grid control. I will not know the exact number of columns that will be fetched at run time. So it should dynamically detect the number of columns and attached the data to the grid. Thank you for your help. Smith

If you really want to bind a data reader you can add this code to a static class

    public static IEnumerable<System.Data.IDataRecord> AsEnumerable(this System.Data.IDataReader reader)
    {
        while (reader.Read())
        {
            yield return reader;
        }
    }

And then just do

myGrid.ItemsSource = myReader.AsEnumerable();

I haven't tried that but it might work. It also might not work for a number of reasons.

However I think you would be better off binding to a DataTable. It's the same 'era' as a DataReader

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM