【问题标题】:asp.Net Gridview shows datatype instead of valueasp.Net Gridview 显示数据类型而不是值
【发布时间】:2013-04-30 06:58:47
【问题描述】:

我遇到了一个小问题,我找不到解决方案。

当 gridview 被绑定时,所有价格都将显示为Microsoft.Xrm.Sdk.Money 而不是它的值。

有谁知道为什么会发生这种情况以及如何改变它?

这是我的 Gridview:

<asp:GridView ID="ProductList" 
          runat="server" 
          AutoGenerateColumns="false" 
          OnRowDataBound="ProductList_OnRowDataBound">
  <Columns>
    <asp:BoundField 
        HeaderText="Productno." 
        DataField="ProductNumber"/>

    <asp:BoundField 
        HeaderText="Product" 
        DataField="Name" />

    <asp:BoundField 
        HeaderText="Price/Unit" 
        DataField="Price" />
  </Columns>
</asp:GridView>

我用下面的代码填充它

Products = (from ppl in priceCatalog.price_level_product_price_levels
            join prod in ServiceContext.ProductSet.ToList()
                 on ppl.ProductId.Id equals prod.ProductId
            select new Product()
            {
                ProductId = prod.ProductId,
                ProductNumber = prod.ProductNumber,
                Name = prod.Name,
                Price = prod.Price,
                //Price = ppl.Amount.Value,
                PriceLevelId = ppl.PriceLevelId,
                SubjectId = prod.SubjectId,
                DefaultUoMId = ppl.UoMId
            }).ToList();

var product = Products.Where(p => p.SubjectId.Id == filterTo).ToList();

ProductList.DataKeyNames = new[] { "ProductId" };
ProductList.DataSource = product.ToDataTable(XrmContext);
ProductList.DataBind();

目前看起来是这样的:

PNo.     Name        Price 
1        Prod 1      Microsoft.Xrm.Sdk.Money
2        Prod 2      Microsoft.Xrm.Sdk.Money
3        Prod 3      Microsoft.Xrm.Sdk.Money
....

在 xrm.cs(为早期绑定 CRM 生成的文件)

如下:

[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("price")]
public System.Nullable<decimal> Price
{
    get
    {
        return this.GetAttributeValue<System.Nullable<decimal>>("price");
    }
    set
    {
        this.SetAttributeValue<Microsoft.Xrm.Sdk.Money>("Price", "price", value);       
    }
}

【问题讨论】:

    标签: asp.net gridview dynamics-crm-2011


    【解决方案1】:

    您可以使用TemplateField 来显示Price 字段的Value 属性(包含十进制值)。我在您的示例中添加了另一列

    <asp:GridView ID="ProductList" 
              runat="server" 
              AutoGenerateColumns="false" 
              OnRowDataBound="ProductList_OnRowDataBound">
      <Columns>
        <asp:BoundField 
            HeaderText="Productno." 
            DataField="ProductNumber"/>
    
        <asp:BoundField 
            HeaderText="Product" 
            DataField="Name" />
    
        <asp:BoundField 
            HeaderText="Price/Unit" 
            DataField="Price" />
    
        <asp:TemplateField HeaderText="Price/Unit Decimal">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Price.Value") %>'></asp:Label>
            </ItemTemplate>
         </asp:TemplateField>
    
      </Columns>
    </asp:GridView>
    

    【讨论】:

    • 现在我收到DataBinding: 'System.String' does not contain a property with the name 'Value'. 似乎在分配该属性时出现问题
    • 如果PriceMicrosoft.Xrm.Sdk.Money 对象,则Value 属性将正确绑定。尝试以这种方式仅绑定价格Bind("Price") 并检查输出
    • 这是一种货币类型,请参阅我的编辑。我会得到类型而不是它的值:-(
    • 你从xrm.cs中获取的代码不正确或者不属于Product.Price,正确的就是这样生成的(请在VS里面复制粘贴):[Microsoft.Xrm .Sdk.AttributeLogicalNameAttribute("price")] public Microsoft.Xrm.Sdk.Money Price { get { return this.GetAttributeValue("price"); } 设置 { this.OnPropertyChanging("Price"); this.SetAttributeValue("价格", value); this.OnPropertyChanged("价格"); } }
    • 不幸的是,这对我没有帮助,但我发现我没有创建新产品(选择),然后它就很好了。有没有更好的方法来获取与价目表相关的产品?
    【解决方案2】:

    我没有找到可行的解决方案。

    我做了什么,所以它起作用了:

    我创建了一个新类,此时我使用了它。现在可以了:

    Products = (from ppl in priceCatalog.price_level_product_price_levels
            join prod in ServiceContext.ProductSet.ToList()
                 on ppl.ProductId.Id equals prod.ProductId
            select new ProductClass()
            {
                ProductId = prod.ProductId,
                ProductNumber = prod.ProductNumber,
                Name = prod.Name,
                Price = prod.Price,
                //Price = ppl.Amount.Value,
                PriceLevelId = ppl.PriceLevelId,
                SubjectId = prod.SubjectId,
                DefaultUoMId = ppl.UoMId
            }).ToList();
    

    【讨论】:

      猜你喜欢
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      相关资源
      最近更新 更多