C# DataGridView返回没有数据的行

C# DataGridView返回没有数据的行,c#,winforms,datagridview,C#,Winforms,Datagridview,我的windows应用程序项目中有一个问题,我担心这是一个非常小的问题,出于某种原因,我无法掌握解决方案 我希望以编程方式在数据网格中显示数据,但它只返回基于从数据库中提取的记录数的行,其中没有数据。下面是我的代码 private void BindDataToGrid() { var teller = new TellerService().GetByUsername(username); var transactions = new PocTransactionService

我的windows应用程序项目中有一个问题,我担心这是一个非常小的问题,出于某种原因,我无法掌握解决方案

我希望以编程方式在数据网格中显示数据,但它只返回基于从数据库中提取的记录数的行,其中没有数据。下面是我的代码

private void BindDataToGrid()
{
    var teller = new TellerService().GetByUsername(username);
    var transactions = new PocTransactionService().GetByTellerId(teller.TellerId);
    BindingList<PocTransaction> trans = new BindingList<PocTransaction>(transactions);
    var source = new BindingSource(trans, null);
    dgvTransactions.DataSource = source;
}

不是在代码中填充变量事务,而是由于某种原因,它没有显示在网格上。这真让我发疯。我相信你们中的一位会给我指明出路。

我终于找到了答案。我最初写的代码没有问题。我只是没有为我在网格上定义的每一列设置DataPropertyName。将它们适当地设置为datasource属性可以获得所需的结果


感谢加布里埃尔迄今为止的帮助。非常感谢。

为什么不使用var source=new BindingSourcetrans;?BindingSource接受的参数不少于两个Gabriel。但感谢您的回复。第二个参数DataMember的默认值是。也许那会有帮助。。在使用BindingSource时,我通常使用默认构造函数,然后设置数据源。Gabriel仍然不起作用。你能分享你用来工作的代码吗。。我以前没有看到,但您正在使用BindingList和BindingSource。。尝试直接绑定绑定列表:dgvTransactions.DataSource=trans;。