在特定时间使用DataBind()

本文关键字:DataBind 定时间 | 更新日期: 2023-09-27 17:49:02

我有一个电子商务网站,它有一些特殊的运输规则。

基本上,某些产品的货运类型根据购物车中所有物品的总成本决定是否免费运输。有多种货运类型(大约13种左右),我需要在购物车中按货运类型排序显示它们(以及某种标识符,如标题)。

当前购物车是一个基于预定字段(productImage、itemId、name等)绑定数据的表。我需要做的是通过它们的货运类型对所有项目进行排序,然后在每个货运类型的开头插入1行,指定它们的货运类型。我目前的方法是检查每个项目的运费类型,然后将它们添加到我想稍后使用DataBind()的列表中。

问题是DataBind()不能在不重写的情况下多次使用。例子:

//This works
try
{
    base.DataSource = cart.Items;
    base.DataBind();
}
//This doesn't work
if (freightTypeOne.Count != 0) {
   try
   {
       base.DataSource = freightTypeOne; //freightTypeOne is a list of Items
       base.DataBind();
   }
   catch 
   {
       //handle exceptions
   }
}    
if (freightTypeTwo.Count != 0) {
   try
   {
       base.DataSource += freightTypeTwo; //freightTypeTwo is a list of Items
       base.DataBind();
   }
   catch 
   {
       //handle exceptions
   }
}

+=不能从我所看到的使用,只是做一个=覆盖信息。在有界数据之间插入新行也有问题。只能插入数据之前或之后的行

所以基本上,使用上面的方法,我能得到这个作为我的最终结果(或类似的东西)吗?
if (freightTypeOne.Count != 0) {
   try
   {
       // add new GridViewRow with TD that has: "Freight Type One" spanning full row
       base.DataSource = freightTypeOne;
       base.DataBind();
   }
   catch 
   {
       //handle exceptions
   }
}    
if (freightTypeTwo.Count != 0) {
   try
   {
       // add new GridViewRow with TD that has: "Freight Type Two" spanning full row
       base.DataSource += freightTypeTwo;
       base.DataBind();
   }
   catch 
   {
       //handle exceptions
   }
}

如果有帮助的话,这些都是在DLL中完成的。数据是从数据库中提取的,该代码位于CreateChildControls()覆盖中。请原谅,我是。net的新手,所以请原谅我。

在特定时间使用DataBind()

base.DataSource = freightTypeOne.Union(freightTypeTwo);
base.DataBind();

在上面的代码中,你不需要检查计数——空集合就可以了。

不是null(即检查你的集合之前不是空的,如果你必须)。

数据源变量为List<T>类型,例如:List<FreightType>。我做这个假设是因为你在你的问题中使用了"列表"这个词(我可能错了)。

注意:您将需要在您的类中System.Linq命名空间,以防您还没有它。