【问题标题】:Datagrid displays schema information instead of actual dataDatagrid 显示架构信息而不是实际数据
【发布时间】:2018-09-06 01:48:12
【问题描述】:

我已经编写了一些代码,用于连接到 WCF 服务并使用对 SQL Server 的存储过程调用显示来自 IEnumerable<Dataset> 的数据。

我使用 WCF 客户端对其进行了测试,它返回:WCF Test client result 我不确定这是否正确,但它似乎是唯一可行的方法。

在 WPF 中,它似乎连接并填充字段,但使用架构信息,而不是数据集中的表。我附上截图供参考。Client interface

我已经看到这个线程告诉 DataGrid 你要设置哪个表: DataGridView not displaying data while DataSet contains the data

但是,这似乎不适用于我的情况,因为IEnumerator<> 不包含方法“表”。但我觉得重点保持不变,即我需要告诉 DataGrid 我真正想要显示的内容。在谷歌上搜索了几个小时后,我终于到了。

我也尝试过声明IQueryable 并使用.Contains 方法,但这也不起作用。

这是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;
    }
}

这是 WCF 接口:

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

这是 WCF 实现:

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

编辑:也试过这个无济于事

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

【问题讨论】:

    标签: c# wpf wcf


    【解决方案1】:

    解决方案是在适当的索引处访问 DataSet,这似乎很明显,然后使用 .Tables 方法访问“Table”表。

    像这样:

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

    这在 Microsoft 文档中的任何地方都没有记录(惊喜)。 *生气只会反应

    我希望这个解决方案在未来对某人有所帮助

    编辑:另外,以防你错过它,我必须在 xaml 中的 DataGrid 中添加 ItemSource 的绑定

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-24
      • 2016-08-20
      • 1970-01-01
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多