无法将类型为“DynamicClass1”的对象强制转换为类型“ET_LINQ.ET_COURSE_MSTR”

今天看了书上的GridView事件的RowDataBound.然后自己写照着做起来了.

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Asp_Net_Study_DataSourceBind : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //this.LinqDataSource1.Select = "new(OrderID,ShipAddress,CustomerID,CustomerID,OrderDate,ShipName)";
        this.GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);
    }

    void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.DataItem != null)
        {
            System.Data.DataRowView drv = (System.Data.DataRowView)e.Row.DataItem;
            if (Convert.ToDecimal(drv["Average_UnitPrice"]) > 24)
            {
                e.Row.BackColor = System.Drawing.Color.Brown;
                e.Row.ForeColor = System.Drawing.Color.White;
            }
        }
    }
}
前台GridView代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="LinqDataSource1">
            <Columns>
                <asp:BoundField DataField="Average_UnitPrice" HeaderText="Average_UnitPrice" 
                    ReadOnly="True" SortExpression="Average_UnitPrice" />
                <asp:TemplateField>
                <ItemTemplate>
                <%# Eval("Categories.CategoryName")%>
                </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
<%--                <ItemTemplate>
                <asp:BulletedList DataSource='<%#Eval("Products")%>'
                 DataTextField="ProductName" runat="server" ID="BulletedList"/>
                </ItemTemplate>--%>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:LinqDataSource ID="LinqDataSource1" runat="server"
        ContextTypeName="DataClassesDataContext" EntityTypeName="" TableName="Products"
         GroupBy="Categories" 
            
            Select="new (key as Categories, it as Products, Average(UnitPrice) as Average_UnitPrice)">
        </asp:LinqDataSource>
我就奇怪怎了报错了.而且第一次看见这种错误.

不过看名字知道是因为字段是动态生成不能转换未DataRowView类型.

刚开始知道问题了.就去看看e.Row.DataItem究竟是什么类型.想就这样转换那种类型可以了..

没想到的是不能这样转换了..

在网上找了下相关的..终于找到解决方法:

void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var val = (Decimal)DataBinder.Eval(e.Row.DataItem, "Average_UnitPrice");

            if (val > 24)
            {
                e.Row.BackColor = System.Drawing.Color.Brown;
                e.Row.ForeColor = System.Drawing.Color.White;
            }
        }
    }

关键就在这里-->(Decimal)DataBinder.Eval(e.Row.DataItem, "Average_UnitPrice");

 

转载于:https://www.cnblogs.com/dancky/archive/2010/11/25/1888231.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值