问问题
1515 次
1 回答
0
抱歉有点长。对于每一个Report
,我们都有多种方法。正如评论部分中提到的,我生成了相同的(不完全是,但结果是相同的)报告,列表如下:
- 用一张表创建一个
DataSet
有 7Column
秒。
- 开始一个新的
Report
,在里面添加一个List
完整的空间,然后在里面添加 7 个TextBox
es(从 Col1 到 Col 7 的数据)List
。
- 设置
Visibility
这些TextBox
es 以便Show or hide based on an expression
在我们传递 emty 字符串数据时隐藏它们:
文本框1:=IIF(Fields!Col1.Value="",TRUE,FALSE)
文本框2:=IIF(Fields!Col2.Value="",TRUE,FALSE)
文本框 3 到 7:=IIF(Fields!Col3.Value & Fields!Col4.Value & Fields!Col5.Value & Fields!Col6.Value & Fields!Col7.Value = "",TRUE,FALSE)
- 示例数据结构:
一些简单的事情:
class clsDataStruct
{
public class ItemType
{
public string Name { get; set; }
}
public class Item
{
public string Name { get; set; }
public ItemType Type { get; set; }
public int Cost { get; set; }
public int Prize { get; set; }
}
public class BillByItem
{
public DateTime DateSold { get; set; }
public Item Item { get; set; }
}
}
- A
Form
查看结果:
添加一个Form
,一个ReportViewer
到那个Form
,设置我们之前创建Report
的那个ReportViewer
。然后在Form
'sLoad
事件中,添加一些代码来生成一些随机数据(这是你取数据的地方)然后生成我们的Report
:
//for data generation
static Random rdn = new Random();
//some list to hold data
List<clsDataStruct.ItemType> lstItemType = new List<clsDataStruct.ItemType>();
List<clsDataStruct.Item> lstItem = new List<clsDataStruct.Item>();
List<clsDataStruct.BillByItem> lstBill = new List<clsDataStruct.BillByItem>();
//this is what user choose to filter the report
//user choose to report which item type
List<clsDataStruct.ItemType> lstItemTypeByUser = new List<clsDataStruct.ItemType>();
//date begin and end of report
DateTime dteStart;
DateTime dteEnd;
private void Form1_Load(object sender, EventArgs e)
{
//create 3 ItemType
for (int i = 1; i < 4; i++)
{
clsDataStruct.ItemType itt = new clsDataStruct.ItemType();
itt.Name = "Item Type " + i.ToString();
lstItemType.Add(itt);
}
//create 12 Item
for (int i = 1; i < 13; i++)
{
clsDataStruct.Item item = new clsDataStruct.Item();
item.Name = "Item " + i.ToString();
item.Type = lstItemType[i % 3];
item.Cost = rdn.Next(10);
item.Prize = item.Cost + rdn.Next(5);
lstItem.Add(item);
}
//create 30 BillByItem in next 3 month
for (int i = 1; i < 31; i++)
{
clsDataStruct.BillByItem bill = new clsDataStruct.BillByItem();
bill.DateSold = DateTime.Now.AddDays(rdn.Next(90));
bill.Item = lstItem[rdn.Next(12)];
lstBill.Add(bill);
}
//set the filters
//add 2 random type to the filter
lstItemTypeByUser.Add(lstItemType.Where(s => !lstItemTypeByUser.Contains(s)).ToList()[rdn.Next(lstItemType.Where(s => !lstItemTypeByUser.Contains(s)).ToList().Count)]);
lstItemTypeByUser.Add(lstItemType.Where(s => !lstItemTypeByUser.Contains(s)).ToList()[rdn.Next(lstItemType.Where(s => !lstItemTypeByUser.Contains(s)).ToList().Count)]);
//date start and end is one month from date we have data
dteStart = DateTime.Now.AddDays(rdn.Next(60) - 30);
dteEnd = DateTime.Now.AddMonths(3).AddDays(rdn.Next(60) - 30);
this.reportViewer1.LocalReport.DataSources.Clear();
dsReports.dtLyLichTrichNgangDataTable dtLyLichTrichNgang = new dsReports.dtLyLichTrichNgangDataTable();
//Simple title, replace with yours
dtLyLichTrichNgang.Rows.Add("ITEM INVENTORY SOLD " + dteStart.ToShortDateString() + " - " + dteEnd.ToShortDateString(), "", "", "", "", "", "");
//empty row
dtLyLichTrichNgang.Rows.Add(" ", "", "", "", "", "", "");
DateTime dteFirstOfCycle;
DateTime dteLastOfCycle;
//cycle through months and fill data to datatable, maybe week or quarter
for (dteFirstOfCycle = new DateTime(dteStart.Year, dteStart.Month, 1); dteFirstOfCycle < dteEnd; dteFirstOfCycle = dteFirstOfCycle.AddMonths(1))
{
dteLastOfCycle = dteFirstOfCycle.AddMonths(1).AddDays(-1);
//take BillByItem in each month
var billMonth = lstBill.Where(s => s.DateSold >= dteFirstOfCycle && s.DateSold <= dteLastOfCycle).OrderBy(s => s.DateSold);
dtLyLichTrichNgang.Rows.Add("FROM " + dteFirstOfCycle.ToShortDateString() + " TO " + dteLastOfCycle.ToShortDateString(), "", "", "", "", "", "");
//empty row
dtLyLichTrichNgang.Rows.Add(" ", "", "", "", "", "", "");
//cycle through each item type
foreach (clsDataStruct.ItemType itt in lstItemTypeByUser)
//have sold something
if (billMonth.Where(s => s.Item.Type == itt).Count() != 0)
{
//itemtype
dtLyLichTrichNgang.Rows.Add(itt.Name.ToUpper(), "", "", "", "", "", "");
//detail header
dtLyLichTrichNgang.Rows.Add("", "", "Name", "Cost", "Prize", "Profit", "Date Sold");
//cycle through each bill
foreach (clsDataStruct.BillByItem bill in billMonth)
dtLyLichTrichNgang.Rows.Add("", "", bill.Item.Name, bill.Item.Cost, bill.Item.Prize, bill.Item.Prize - bill.Item.Cost, bill.DateSold.ToShortDateString());
//total row
dtLyLichTrichNgang.Rows.Add("", "", "TOTAL", billMonth.Sum(s => s.Item.Cost), billMonth.Sum(s => s.Item.Prize), billMonth.Sum(s => s.Item.Prize - s.Item.Cost), "");
}
//sold nothing
else
{
dtLyLichTrichNgang.Rows.Add(itt.Name.ToUpper(), "", "", "", "", "", "");
dtLyLichTrichNgang.Rows.Add("Nothing sold", "", "", "", "", "", "");
//empty row
dtLyLichTrichNgang.Rows.Add(" ", "", "", "", "", "", "");
}
//empty row
dtLyLichTrichNgang.Rows.Add(" ", "", "", "", "", "", "");
}
this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("dsLyLichTrichNgang", (DataTable)dtLyLichTrichNgang));
this.reportViewer1.RefreshReport();
}
- 结果:
通过一些调整TextBox
你BorderStyle
可以有你想要的Report
。
于 2015-10-10T03:33:33.687 回答