本文介绍了从ReportViewer中的DataGridView显示数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 根据先前有关报表查看器中动态列的问题,我认为我至少会尝试使事情正常进行。 显示的输出是数学因子的列,每个列都有自己的唯一名称。这些因素(可以选择1到10之间的任何数字)在我的DataGridView控件中显示没有问题-通过根据需要添加行/列来生成DataTable,然后将数据表设置为DataGridView的数据源。 我已将ReportViewer添加到WinForm,并生成了RDLC文件。我的RDLC不用花数小时试图弄清楚如何使其动态,而是设置了10列(据我所知,永远不会超过此列),并且将DataSet分配给了ReportViewer控件。然后,我使用以下代码将数据分配给查看器:Following on from an earlier question about dynamic columns in a report viewer, I thought I would at least attempt to get things working.The output being displayed are columns of mathematical factors, each with their own unique name. These factors (any number from 1 to 10 can be selected) display without issue in my DataGridView control - I generate a DataTable by adding rows/columns as required, and then set the datatable as the Datasource for the DataGridView.I have added a ReportViewer to my WinForm, and generated a RDLC file. Rather than spend hours trying to figure out how to make it dynamic, my RDLC is set up with 10 columns (as I know there will never be more than this), and the DataSet is assigned to my ReportViewer control. I then use the following code to assign the data to the viewer:DataTable dt = new DataTable();var source = this.dgvSelectedElements.DataSource;while (source is BindingSource){ source = ((BindingSource)source).DataSource;}var table = source as DataTable;if (table != null){ dt = table; var reportSource = new ReportDataSource("FactorReport", dt); this.reportViewer1.Reset(); this.reportViewer1.ProcessingMode = ProcessingMode.Local; this.reportViewer1.LocalReport.ReportPath = "FactorReport.rdlc"; this.reportViewer1.LocalReport.DataSources.Clear(); this.reportViewer1.LocalReport.DataSources.Add(reportSource); this.reportViewer1.RefreshReport();}因此,DataGridview的外观如下: 然后,这是Reportviewer-请注意,两个 Rate和 Mult行显示在底部 因此,尽管它似乎可以正常工作,但我无法在网格中看到这些值。另外,当年龄超过100时,年龄分类不正确-可以关闭分类功能吗?并假设我可以使它起作用,是否可以遍历ReportViewer并修改列标题?快速的Google告诉我不是在运行时吗?So this is how the DataGridview looks:And then, this is the Reportviewer - note the two 'Rate' and 'Mult' rows appear at the bottomSo whilst it appears to work, I cannot see the values in the grid. Also, when the ages exceed 100, the ages are sorted incorrectly - can sorting be turned off? And on the assumption I can get this to work, is it possible to iterate through the ReportViewer and amend the column captions? A quick google tells me not at run-time?推荐答案也许不是最好/最快的解决方案,但它可以工作,并且需要一些额外的帮助更改可见性表达式后,我只能显示已选择的列数。 我在名为 Factor_Name_1 的报表查看器中向 Factor_Name_10添加了10个参数,并在创建属性时在属性中选中了允许空值。然后,我编写了一些代码以添加参数数据,存储现有的列名称,然后将它们重命名为我的Factor1,Factor2 ... Factor10标题,如下所示(请注意,我不担心在第一个 Age列中使用): / p>Maybe not the best/quickest solution but it works and with some additional changes to the visibility expressions, I'm able to only show the number of columns that have been selected.I added 10 parameters to my reportviewer named Factor_Name_1 to Factor_Name_10, and checked 'Allow null value' in the properties when creating them. I then wrote some code to add the data for the parameters, storing the existing column names before renaming them to my Factor1, Factor2...Factor10 headings as follows (note I am not worried about in the first 'Age' column): // loop through the columns to create parameters... for (int i = 1; i < dt.Columns.Count; i++) { // get the name of the column... string factorName = dt.Columns[i].ColumnName; // define the new column name... string colName = string.Format("Factor_Name_{0}", i); // change the column name now we have stored the real name... dt.Columns[i].ColumnName = string.Format("Factor{0}", i); // generate the parameter... var rparam = new ReportParameter(colName, factorName); // add it to the parameter set for the control... this.reportViewer1.LocalReport.SetParameters(new[] { rparam }); }在创建关联参数后,通过重新设置列名,数据将正确绑定。在RDLC设计器中,我要做的就是用相关的参数值替换现有的标题。然后,要隐藏未选中的列,我将参数和行文本框的可见性表达式设置为:By re-setting the column names once I've created the associated parameter, the data will then bind correctly. All I had to do then, in my RDLC designer, is to replace the existing headings with the associated parameter values. Then, to hide the columns not selected, I set the visibility expression for the Parameter and Row Textboxes to:=IIF(ISNOTHING(Parameters!Factor_Name_1.Value),True,False)........=IIF(ISNOTHING(Parameters!Factor_Name_10.Value),True,False)现在,如果我选择1或10列,查看器将显示正确的列数并隐藏不需要的列。现在正在重复年龄列(已经重复了标题)并在运行时添加页眉/页脚,但是我目前有一个报告,以正确的标题显示了我需要的数据。 感谢@Reza Aghaei使我走上正轨。Now, if I choose 1 or 10 columns, the viewer displays the correct number of columns and hides those not required. Am now working on repeating the 'age' column (already have the headings repeating) and adding a header/footer at runtime, but I currently have a report that displays the data I need with the correct headings.Thanks to @Reza Aghaei for getting me on the right track. 这篇关于从ReportViewer中的DataGridView显示数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-09 21:36