2012-12-26 16 views
0

这是我第一次使用DataSets和BindingSources,所以请对我温柔。作为一个更复杂的报告系统的一部分(我已经将它蒸馏成一个基本的化身,但它仍然不会正确运行),我试图从数据库使用DataSet拉数据有问题(即是不是通过设计师设置的)。这里是我到目前为止的代码:我这个基础上我的了.Designer.cs文件看到设置通过设计师结合后的代码使用BindingSource将DataSet链接到DataGridView,但没有数据

// pull the data set 
var dsReportData = new Reports2.ReportTest2(); 
dsReportData.BeginInit(); 
dsReportData.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema; 

// bind tha data set 
BindingSource bsReportBinding = new BindingSource(); 
((ISupportInitialize)bsReportBinding).BeginInit(); 
bsReportBinding.DataMember = dsReportData.Tables[0].TableName; 
bsReportBinding.DataSource = dsReportData; 
bsReportBinding.ResetBindings(true); 

// test this stuff 
dgvTester.DataSource = bsReportBinding; 

dsReportData.EndInit(); 
((ISupportInitialize)bsReportBinding).EndInit(); 

。 dgvTester只是一个DataGridView,其中包含在设计器中创建的默认属性。

ReportTest2数据集只有一个TableAdapter,通过设计器添加。如果我去VS中的数据 - >预览数据并预览了​​3210对象,它就会返回数据,就像我运行用于在SQL Server Management Studio中创建TableAdapter的查询一样。

但是,运行实际的代码会导致DataGridView从查询结果中获取列名称,而不是实际的数据。调试器显示dsReportData.payments.Rows.Count == 0(并且,是的,dsReportData.Tables [0]是付款)。

我最终打算使用BindingSource向ReportViewer提供数据,但首先要确保在去调试报表之前检索数据没有问题。

希望我错过了这里明显的东西。我希望如此...

回答

1

经过一些试验和错误后发现。这是我缺少的一部分:

var TableAdapter = new Reports2.ReportTest2TableAdapters.paymentsTableAdapter(); 
TableAdapter.Fill(dsReportData.payments); 

我没有看到它在我引用因为设计师悄悄入.cs文件代替了.Designer.cs文件的代码。这使得数据出现。

相关问题