C# 如何在dataGrid中有效地查询多个表以生成excel样式的报表
我正在创建一个C#,windows窗体应用程序,使用Access数据库作为后端。我使用数据集和表适配器将信息从Excel传输到数据库,然后使用winforms从数据库中提取所需的记录 我从多个表中检索信息以生成月度销售报告。Store#从组合框中选择的每个报告都需要:C# 如何在dataGrid中有效地查询多个表以生成excel样式的报表,c#,ms-access,datagridview,report,C#,Ms Access,Datagridview,Report,我正在创建一个C#,windows窗体应用程序,使用Access数据库作为后端。我使用数据集和表适配器将信息从Excel传输到数据库,然后使用winforms从数据库中提取所需的记录 我从多个表中检索信息以生成月度销售报告。Store#从组合框中选择的每个报告都需要: 作为行的产品名称(来自产品表) 产品表中有一个字段 称为productAnalysis,已标记 如果产品应该是 包括在分析报告中 期初(库存)盘点 (由日期选择器框选择) (开始日期、产品) 购买价格) 购买的每个项目的数量。(
- 作为行的产品名称(来自产品表) 产品表中有一个字段 称为productAnalysis,已标记 如果产品应该是 包括在分析报告中
- 期初(库存)盘点 (由日期选择器框选择)
- (开始日期、产品) 购买价格)
- 购买的每个项目的数量。(因此 按产品#/商店/日期列出的数量总和 从交易记录表中)
- 期末(存货)盘点 (由日期选择器框选择)
- 产品购买的截止日期 价格
DataGridView
我将根据所有这些信息计算一些字段。我所能找到的所有信息都适用于使用SQL Server,但我还无法将这些示例应用于我的情况。我可以将其分为若干块,并分别进行查询,但将其全部放在一个位置对我来说是有问题的。下面是我尝试过的一些问题
SELECT AnalysisItems.ProductName,
BeginningCount.CountNumber AS Beginning,
sum(Transactions.TransactionQty) AS SumOfTransactionQty,
EndingCount.CountNumber AS Ending
FROM Product
INNER JOIN (((AnalysisItems
INNER JOIN BeginningCount ON AnalysisItems.Pmid = BeginningCount.Pmid)
INNER JOIN EndingCount ON BeginningCount.Pmid = EndingCount.Pmid)
INNER JOIN Transactions ON EndingCount.Pmid = Transactions.Pmid)
ON (AnalysisItems.Pmid = Product.Pmid) AND (Product.Pmid = Transactions.Pmid)
WHERE (((Product.ProductAnalysis)=1))
GROUP BY AnalysisItems.ProductName, BeginningCount.CountNumber,
EndingCount.CountNumber;
这让我更接近了,但对于相同的产品,仍然有多个条目。我只是不知道如何在access中实现这一点
SELECT
AnalysisItems.ProductName,
BeginningCount.CountNumber AS Beginning,
Sum(Transactions.TransactionQty) AS SumOfTransactionQty,
EndingCount.CountNumber AS Ending,
EndingCount.StoreAccount
FROM Product
INNER JOIN
(
AnalysisItems
INNER JOIN BeginningCount ON
AnalysisItems.Pmid = BeginningCount.Pmid
INNER JOIN EndingCount ON
BeginningCount.Pmid = EndingCount.Pmid
INNER JOIN Transactions ON
EndingCount.Pmid = Transactions.Pmid
) ON
(Product.Pmid = AnalysisItems.Pmid) AND
(Product.Pmid = Transactions.Pmid)
WHERE (((Product.ProductAnalysis)=1))
GROUP BY
AnalysisItems.ProductName,
BeginningCount.CountNumber,
EndingCount.CountNumber,
EndingCount.StoreAccount;
也许有一个更简单的方法,但我似乎找不到。虽然我是一个初学者,但总有一天我会回顾过去,并想这是多么容易,我确信:)我所做的是 将3个DataGridView框放在一个窗体上。在屏幕加载时,我将“以前的”月份数据加载到第一个月份,将当前月份数据加载到第二个月份。我将它们最小化并锁定在第三个网格后面,这样它们就看不见了。然后,我使用前两个网格中的每一行创建一个dataTable。执行所需的计算,同时将所有计算结果添加到第三个表中
private void btnLoad_Click(object sender, EventArgs e)
{
//to load the first chosen months data
this.beginningCountTableAdapter.Fill(this.beginningDataSet.BeginningCount, Convert.ToInt32(storeAccountComboBox.Text), Convert.ToDateTime(dateTimePicker1.Text));
//to load the second chosen months data
this.endingCountTableAdapter.Fill(this.endingDataSet.EndingCount, Convert.ToInt32(storeAccountComboBox.Text), Convert.ToDateTime(dateTimePicker2.Text));
//Create new DataTable
DataTable dtItems = new DataTable();
//add columns to the dataTable
dtItems.Columns.Add("Product", typeof(string));
dtItems.Columns.Add("Beginning", typeof(decimal));
dtItems.Columns.Add("Purchases", typeof(int));
dtItems.Columns.Add("Ending", typeof(decimal));
//Add a new Column to Calculate the Value
dtItems.Columns.Add("Total Gone (Usage)", typeof(decimal));
//add more columns for $ value
dtItems.Columns.Add("Beginning$", typeof(decimal));
dtItems.Columns.Add("Purchases$", typeof(decimal));
dtItems.Columns.Add("Ending$", typeof(decimal));
//Add a new Column to Calculate the Value
dtItems.Columns.Add("Total Gone ($)", typeof(decimal));
dtItems.Columns.Add("% of Sales", typeof(decimal));
try
{
for (int i = 0; i < dataGridView1.RowCount; i++)
{
//first column
string product = Convert.ToString(dataGridView1.Rows[i].Cells[0].Value);
//calculation for TotalGone (usage)
decimal beginning = Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value);
decimal purchases = Convert.ToDecimal(dataGridView2.Rows[i].Cells[0].Value);
decimal ending = Convert.ToDecimal(dataGridView2.Rows[i].Cells[1].Value);
decimal totalGone = ((beginning + purchases) - ending);
//Variables for conversions for $ column math
decimal beg = Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value);
decimal begPrice = Convert.ToDecimal(dataGridView1.Rows[i].Cells[2].Value);
decimal begValue = beg * begPrice;
decimal pur = Convert.ToDecimal(dataGridView2.Rows[i].Cells[0].Value);
decimal purPrice = Convert.ToDecimal(dataGridView2.Rows[i].Cells[2].Value);
decimal purValue = pur * purPrice;
decimal end = Convert.ToDecimal(dataGridView2.Rows[i].Cells[1].Value);
decimal endPrice = Convert.ToDecimal(dataGridView2.Rows[i].Cells[2].Value);
decimal endValue = end * endPrice;
decimal totalGoneValue = ((beg * begPrice) + (pur * purPrice)) - (end * endPrice);
totalGoneColumnTotal = totalGoneColumnTotal + totalGoneValue; //The total of all product totalGoneValues
decimal salesPercentage = 0; //The sales percentage
thirtyPercent = Convert.ToDecimal(txtThirtyPct.Text); //the thirty percent discount txt entry
double chickenValue = Convert.ToDouble(dataGridView2.Rows[28].Cells[0].Value);
chickenRebate = (chickenValue * .512);
double steakValue = Convert.ToDouble(dataGridView2.Rows[29].Cells[0].Value);
steakRebate = steakValue * .550;
totalWithRebates = (Convert.ToDouble(totalWithDiscount) - ((chickenRebate + steakRebate)));
try
{
decimal sales = Convert.ToDecimal(textSales.Text); //convert the sales entry
salesPercentage = totalGoneValue / sales * 100; //find sales percentage
totalWithDiscount = totalGoneColumnTotal - thirtyPercent; //find thirtypercent discount amount
}
catch { MessageBox.Show("The sales entry must be a number between 1 and 999999.99");
break;
}
//add colors to the output.
//Add rows to the DataTable
dtItems.Rows.Add(product, beginning, purchases, ending, totalGone, begValue, purValue, endValue, totalGoneValue, salesPercentage);
}//end for
//Totals Summary Rows
dtItems.Rows.Add("Totals", null, null, null, null, null, null, null, totalGoneColumnTotal, null);
dtItems.Rows.Add("30% discounts", null, null, null, null, null, null, null, thirtyPercent ,null);
dtItems.Rows.Add("Total w/discount", null, null, null, null, null, null, null, totalWithDiscount, null);
dtItems.Rows.Add("Chicken Rebate x * 5.12", null, null, null, null, null, null, null, chickenRebate, null);
dtItems.Rows.Add("Steak Rebate x * 5.50", null, null, null, null, null, null, null, steakRebate, null);
dtItems.Rows.Add("Total w/Rebates", null, null, null, null, null, null, null, totalWithRebates, null);
//Set the DataTable as DataSource of the GridView
dataGridView3.DataSource = dtItems;
}
catch { MessageBox.Show("Could not determine values. There may be no data for this date range/store."); }
private void btnLoad\u单击(对象发送方,事件参数e)
{
//加载第一个选择的月份数据
this.beginningCountTableAdapter.Fill(this.beginningDataSet.BeginningCount,Convert.ToInt32(storeAccountComboBox.Text),Convert.ToDateTime(dateTimePicker1.Text));
//加载第二个选择的月份数据
this.endingCountTableAdapter.Fill(this.endingDataSet.EndingCount,Convert.ToInt32(storeAccountComboBox.Text),Convert.ToDateTime(dateTimePicker2.Text));
//创建新数据表
DataTable dtItems=新DataTable();
//向数据表中添加列
添加(“产品”,类型(字符串));
dtItems.Columns.Add(“开始”,类型(十进制));
添加(“采购”,类型(int));
dtItems.Columns.Add(“Ending”,typeof(decimal));
//添加新列以计算值
dtItems.Columns.Add(“使用总量)”,typeof(decimal));
//为$value添加更多列
dtItems.Columns.Add(“开头$”,typeof(decimal));
dtItems.Columns.Add(“Purchases$”,typeof(decimal));
dtItems.Columns.Add(“Ending$”,typeof(decimal));
//添加新列以计算值
dtItems.Columns.Add(“总计($)”,typeof(十进制));
dtItems.Columns.Add(“%的销售额”),typeof(十进制));
尝试
{
对于(int i=0;i