Search code examples
c#wpfwcf

Datagrid displays schema information instead of actual data


I have written some code in for connecting to a WCF service and displaying the data from an IEnumerable<Dataset> using a stored procedure call to SQL Server.

I tested it using the WCF client and it returns this: WCF Test client result I'm not sure if that is even correct, but it's the only thing that seems to work.

In the WPF, it seems to connect to and populate the fields, but with the schema information, not the tables from the dataset. I have attached a screenshot for reference.Client interface

I have seen this thread on telling the DataGrid which table you want to set: DataGridView not displaying data while DataSet contains the data

However, that does not seem to apply to my situation as IEnumerator<> does not contain a method 'Tables'. But I feel like the point remains the same i.e. I need to tell the DataGrid what I actually want to show. After googling for hours I am finally here.

I have also tried declaring an IQueryable and using the .Contains method, but that didn't work either.

Here is the code in MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    // private CollectionViewSource CollectionView;
    public MainWindow()
    {
        InitializeComponent();
        FillDataGrid();
    }
                         
    private void FillDataGrid()
    {
        MBMService.IMBMExtClientService Client = new MBMService.MBMExtClientServiceClient();
        IEnumerable<DataSet> ds;
        ds = Client.GetData();
        MainGrid.ItemsSource = ds;
    }
}

This is the WCF interface:

[ServiceContract]
public interface IMBMExtClientService
{
    [OperationContract]
    IEnumerable<DataSet> GetData();
}

This is the WCF implementation:

public class MBMExtClientService : IMBMExtClientService
{
    public IEnumerable<DataSet> GetData()
    {
        //call to NYSESQLConnect class and run constructor
        NYSESQLConnect Connect = new NYSESQLConnect();
        return Connect.GetDataSet();
    }
}

EDIT: Also tried this to no avail

 MBMService.IMBMExtClientService Client = new MBMService.MBMExtClientServiceClient();
        DataSet[] ds = Client.GetData();
        //MainGrid.ItemsSource = ds[0];
        MainGrid.DataContext = ds.ElementAt(0).Tables["NYSEStockData"];

Solution

  • The solution was to access the DataSet at the appropriate index, which is seemingly obvious, then access the "Table" table using the .Tables method.

    Like so:

    MainGrid.DataContext = ds[0].Tables["Table"];
    

    This is not documented anywhere in Microsoft docs (surprise, surprise). *angry reacts only

    I hope this solution helps someone in the future

    EDIT: Also, just in case you miss it, I had to add Binding for ItemSource in DataGrid in xaml

     <Grid>
            <DataGrid Name="MainGrid" ItemsSource="{Binding}" Height="390"/>
     </Grid>