Gridview中重复DropDownList选择项的行的总和值
我的库存应用程序中有一个GridView ...我正在使用该网格视图产品出...其工作正常,但我想要检查的东西是检查如果同样的供应商的相同产品在gridview中被选中多次,就像我在图片中所做的那样,那么总和其数量以确保它小于其库存。 对于单一产品将只与它的股票进行比较,但对于每一个重复的产品,将被添加量的值,并将其与股票比较.. 任何帮助将高度赞赏... Gridview中重复DropDownList选择项的行的总和值
如电脑区域的键盘被选中两次,它有一个股票15 ...我想总结数量10 + 7 = 17,并抛出一条消息,如果它大于它的股票是15,否则将其保存为一行... 并且计算机区域的笔被选择了两次(它可以是三次或更多)并且其具有50 ...数量10 + 60 = 70的股票,并且如果它大于它的股票是50,则投掷消息...
你可以在Ro上做到这一点wDataBound的gridview很容易。
只需要获取数量,库存,产品和供应商。但是你也可以通过下面的函数来做到这一点,它会给你的记录,如果在gridview中有重复的东西。
public void HighlightDuplicate(GridView gridview)
{
for(int currentRow = 0; currentRow < gridview.Rows.Count - 1; currentRow++)
{
GridViewRow rowToCompare = gridview.Rows[currentRow];
for (int otherRow = currentRow + 1; otherRow < gridview.Rows.Count; otherRow++)
{
GridViewRow row = gridview.Rows[otherRow];
bool duplicateRow = true;
//example: check Duplicate on column vendor(cell#0) and product(cell#1)
if ((rowToCompare.Cells[0].Text) != (row.Cells[0].Text) && (rowToCompare.Cells[1].Text) != (row.Cells[1].Text))
{
duplicateRow = false;
}
else if (duplicateRow)
{
rowToCompare.Cells[1].Text = Convert.ToInt32(row.Cells[1].Text) + Convert.ToInt32(rowToCompare.Cells[1].Text);
row.Visible=false;
}
}
}
}
在此功能中,您可以检查我们是在比较行和里面我们有电池,并检查电池值。
if ((rowToCompare.Cells[0].Text) != (row.Cells[0].Text) && (rowToCompare.Cells[1].Text) != (row.Cells[1].Text))
{
duplicateRow = false;
}
else if (duplicateRow)
{
rowToCompare.Cells[1].Text = Convert.ToInt32(row.Cells[1].Text) + Convert.ToInt32(rowToCompare.Cells[1].Text);
row.Visible=false;
}
在本部分中,我们比较单元索引0和1,如果相同意味着我们有供应商和产品相同,这些不应该相同。如果供应商和产品是相同的,那么他们的价值和设置只有一行,第二行我设置可见虚假,你可以执行一些其他操作,如果你想,也可以在添加两个行值后检查,如果数量更大或减少,你可以做任何相应的操作。 你可以在DataBind()之后调用这个函数。例如,如果GridView的ID是Gridview1则:
HighlightDuplicate(this.GridView1);
你可以使用JavaScript或者jQuery来计算同一产品的总量,然后将其与该产品的现有库存进行比较。 followling示例使用jQuery。
要进行计算,首先应为网格中的所有项目指定名称或类别。示例行应该看起来像这样。
<tr>
<td class="no">XX</td>
<td><select class="vendor" >... </select></td>
<td><select class="product" >...</select></td>
<td><input class="stock" value="YY" readonly="true"></td>
<td><input class="quantity" value="ZZ"></td>
</tr>
然后你的项目的变化事件添加处理程序数量
$('.quantity').change(function(e){
var quantity = $(this);
var row = quantity.closest("tr");
var selectedVendor = $(".vendor option:selected", row).val();
var selectedProduct = $(".product option:selected", row).val();
// Get all row that have the same vendor and product
var sameRows = $(".product option:selected[value='" + selectedProduct + "']", $(".vendor option:selected[value='" + selectedVendor + "']").closest("tr")).closest("tr");
// Calculate total quantity of same product
var total = 0;
sameRows.each(function() {
total += parseInt($(".quantity", this).val());
});
// Compare total quantity and stock
var stock = parseInt($(".stock", row).val());
if (total > stock) {
alert("Quantity access available stock");
quantity.focus();
}
});
你也应该考虑增加类似的处理程序下拉供应商和产品。
我创建了一个演示here,你可以检查。