c# - How to collect values into an array and pass the collection to datagridview in C#?
问题描述
I am working on a custom POS application in Asp.Net using C# where I have two DataGridViews on a windows form. The DataGridView1 is showing all sales. When a user clicks on any record in DataGridView1, certain details like ProductName, Quantity, Variation, UnitPrice and TotalCost from the selected record on DataGridView is passed to the DataGridView2. This is done using the CurrentCellChanged event in DataGridView2.
My problem is that I have more than one details related to each sale which I am trying to show on DataGridView2 but I can't set each detail of a selected sale as the DataSource for DataGridView2. Here is the code I have tried so far:
private void DataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
try
{
string saleId = DataGridView1.CurrentRow.Cells[0].Value.ToString();
DataGridView2.DataSource = SalesClass.SelectedSaleDetails(saleId);
}
catch (Exception)
{
}
}
Here, the CurrentCellChanged event pass the selected SaleId to my SalesClass where I am querying sales details related to this specific SaleId, like this:
public static DataTable SelectedSaleDetails(string saleId)
{
DataTable dt = new DataTable();
dt = DataAccess.Select(string.Format("select * from vSalesDetails where SaleID = {0}", saleId));
string[] values;
foreach (DataRow drItem in dt.Rows)
{
string variation = drItem["ProductVariation"].ToString();
string productName = drItem["ProductName"].ToString();
decimal quantity = Convert.ToDecimal(drItem["SaleQuantity"]);
decimal saleCostLevelC = Convert.ToDecimal(drItem["LevelCCost"]);
decimal purchaseCostLevelC = Convert.ToDecimal(drItem["LevelCPurchaseCost"]);
decimal levelBStandard = Convert.ToDecimal(drItem["LevelBStandard"]);
decimal levelCStandard = Convert.ToDecimal(drItem["LevelCStandard"]);
decimal parentTotalSalesCost = 0;
decimal levelATotalSalesCost = 0;
decimal levelBTotalSalesCost = 0;
decimal levelCTotalSalesCost = 0;
decimal salecost = 0;
decimal unitCost = 0;
switch (variation)
{
case "Parent Product":
decimal soleProductUnitPrice = Convert.ToDecimal(drItem["SoleProductUnitPrice"]);
salecost = quantity * soleProductUnitPrice;
unitCost = soleProductUnitPrice;
break;
case "Level A":
decimal levelBSaleCost = levelCStandard * saleCostLevelC;
decimal levelASaleCost = levelBStandard * levelBSaleCost;
salecost = quantity * levelASaleCost;
unitCost = levelASaleCost;
break;
case "Level B":
decimal saleCostLevelB = levelCStandard * saleCostLevelC;
salecost = quantity * saleCostLevelB;
unitCost = saleCostLevelB;
break;
case "Level C":
salecost = quantity * saleCostLevelC;
unitCost = saleCostLevelC;
break;
default:
break;
}
values = new string[] {productName, variation, unitCost.ToString(), quantity.ToString(), salecost.ToString()};
}
// return each of the productName, variation, unitCost and quantity of a selected Sale here. This is going to be assigned as the DataGridView2 DataSource.
}
My code is working fine and I am able to get all the required values in values
array within the foreach loop. However, I am somehow not able to pass sale details (productName, variation, unitCost and quantity) of a specific sale back to the DataGridView2 DataSource here:
DataGridView2.DataSource = //values returned from SalesClass;
Is there anyone who can suggest me a better yet efficient approach for this as I am a newbie here.
解决方案
您是否查看过DataGridView的文档?看起来您需要在声明的BindingSource
旁边声明 aDataGridView1
和DataGridView2
。
private BindingSource bindingSource1 = new BindingSource();
然后将您的代码更改为
string saleId = DataGridView1.CurrentRow.Cells[0].Value.ToString();
bindingSource1.DataSource = SalesClass.SelectedSaleDetails(saleId);
DataGridView2.DataSource = bindingSource1;
不要string[]
从SalesClass.SelectedSaleDetails
. 您想退回您的DataTable
dt
.
推荐阅读
- php - 如何根据某些条件调用不同的 WP 页面?
- qt - QT Creator 构建 qml 应用程序
- r - 如何通过将第一行除以第三行来创建行
- autoit - 如何使用 Autoit 中的窗口信息工具捕获窗口标题
- sql-server - 下划线之间的子字符串 - SQL Server 2008
- c# - 在查询中格式化或插入换行符
- python - 如何显示从 node.js 调用的 python 脚本的输出
- ios - NotificationCenter#addObserver(forName:object:queue:using:) 如何工作?
- tableau-api - 计算硬币的价值(加密货币)
- python - 保持数据框中多列重复项的最低值