AR只会再运行时才会加载数据,所以你需要先查询该数据表,返回所有的列,并添加到Fields对象中,示例代码:
- //创建并设置数据源对象
- GrapeCity.ActiveReports.PageReportModel.DataSource myDataSource = new GrapeCity.ActiveReports.PageReportModel.DataSource();
- myDataSource.Name = "Example Data Source";
- myDataSource.ConnectionProperties.DataProvider = "OLEDB";
- myDataSource.ConnectionProperties.ConnectString = connectionstring;
-
- //创建并设置数据集对象
- GrapeCity.ActiveReports.PageReportModel.DataSet myDataSet = new GrapeCity.ActiveReports.PageReportModel.DataSet();
- GrapeCity.ActiveReports.PageReportModel.Query myQuery = new GrapeCity.ActiveReports.PageReportModel.Query();
- myDataSet.Name = "Example Data Set";
- myQuery.DataSourceName = "Example Data Source";
- myQuery.CommandType = GrapeCity.ActiveReports.PageReportModel.QueryCommandType.TableDirect;
- myQuery.CommandText = GrapeCity.ActiveReports.Expressions.ExpressionInfo.FromString("Products");
- myDataSet.Query = myQuery;
-
- // 填充数据集中的Fields
- using (System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(connectionstring))
- {
- DataTable dt = new DataTable();
- System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter("SELECT TOP 1 * FROM Products", connection);
- connection.Open();
- adapter.Fill(dt);
- connection.Close();
- foreach (DataColumn col in dt.Columns)
- {
- GrapeCity.ActiveReports.PageReportModel.Field _field = new GrapeCity.ActiveReports.PageReportModel.Field(col.Caption, col.Caption, null);
- myDataSet.Fields.Add(_field);
- }
- }
复制代码 |