特定时间的DataBind()

时间:2016-06-24 14:13:04

标签: c# asp.net data-binding

我的电子商务网站有一些特殊的送货规则。

基本上,某些产品的运费类型根据购物车中所有商品的总成本确定是否免运费。有多种货运类型(大约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
   }
}

+ =无法从我看到的内容中使用,只是在做一个=覆盖信息。我也无法在此有界数据之间插入新行。 我只能在数据之前或之后插入行。

所以基本上,使用上述方法,我能否将此作为我的最终结果(或类似的东西)?

mysql> SELECT CONVERT_TZ(NOW(), 'America/Chicago', 'GMT');

如果有帮助的话,这都是在DLL中完成的。数据从数据库中提取,此代码位于CreateChildControls()覆盖中。原谅我,但我对.NET很陌生,所以对我很轻松。

1 个答案:

答案 0 :(得分:2)

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

使用上面的代码,您不需要检查计数 - 对于空集合,它没问题。

null没有问题(例如,如果必须,请检查你的馆藏在联盟之前是否为空)。

<强>假设

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

注意:  您已经在课堂上需要System.Linq命名空间,以防您还没有。{/ p>