【问题标题】:Trouble with LINQ databind to GridView and RowDataBoundLINQ 数据绑定到 GridView 和 RowDataBound 的问题
【发布时间】:2010-02-06 12:29:33
【问题描述】:

大家好,

我正在使用 VS 2008 重新设计我的个人网站,并选择使用 LINQ 通过数据访问层创建。我网站的一部分将是一个小应用程序,以帮助更好地管理我的预算。我的第一个 LINQ 查询确实成功执行并显示在 GridView 中,但是当我尝试使用 RowDataBound 事件来处理结果并稍微改进它们时,我得到了错误:

找不到类型或命名空间名称“var”(您是否缺少 using 指令或程序集引用?)

这个有趣的部分是,如果我只是尝试在同一个文件中的其他任何地方输入var s = "s";,我也会得到同样的错误。如果我转到 web 项目中的其他文件,var s = "s"; 编译正常。

这是 LINQ 查询调用:

public static IQueryable pubGetRecentTransactions(int param_accountid)
{
    clsDataContext db;

    db = new clsDataContext();

    var query = from d in db.tblMoneyTransactions
                join p in db.tblMoneyTransactions on d.iParentTransID equals p.iTransID into dp
                from p in dp.DefaultIfEmpty()
                where d.iAccountID == param_accountid
                orderby d.dtTransDate descending, d.iTransID ascending
                select new
                {
                    d.iTransID,
                    d.dtTransDate,
                    sTransDesc = p != null ? p.sTransDesc : d.sTransDesc,
                    d.sTransMemo,
                    d.mTransAmt,
                    d.iCheckNum,
                    d.iParentTransID,
                    d.iReconciled,
                    d.bIsTransfer
                };

    return query;
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.prvLoadData();
    }
}

internal void prvLoadData()
{
    prvCtlGridTransactions.DataSource = clsMoneyTransactions.pubGetRecentTransactions(2);

    prvCtlGridTransactions.DataBind();
}


protected void prvCtlGridTransactions_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var datarow = e.Row.DataItem;
        var s = "s";

        e.Row.Cells[0].Text = datarow.dtTransDate.ToShortDateString();
        e.Row.Cells[1].Text = datarow.sTransDesc;
        e.Row.Cells[2].Text = datarow.mTransAmt.ToString("c");
        e.Row.Cells[3].Text = datarow.iReconciled.ToString();
    }//end if
}//end RowDataBound

到目前为止,我的谷歌搜索还没有找到一个好的答案,所以我把它交给了这个值得信赖的社区。感谢您抽出宝贵时间为我提供帮助。

【问题讨论】:

  • 这是一个网站项目还是网络应用项目?

标签: c# linq-to-sql data-binding gridview anonymous-types


【解决方案1】:

对我来说似乎很奇怪,var 是一个语言关键字,但编译器在这种情况下将它视为一种类型。既然您已经(成功地)在pubGetRecentTransactions() 中使用了var,我假设您正在针对.NET 3.5 进行编译,对吧?

VS 有时会做一些很奇怪的事情。您是否尝试过重新启动 VS 并在之后进行完全重建?

【讨论】:

  • 是的,我检查了 Web 项目,它的目标设置为 3.5。刚刚尝试重新启动并进行完全重建 - 同样的错误。
  • 新信息:我删除了给我错误的 "var" 行,以及 var s = "s";以前看起来很好的行然后也开始抛出编译器错误。所以我注释掉了所有的变量以便我可以编译,并添加了一个 到其中一个 aspx 页面,并得到相同的错误,但发现特别有趣: C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE> "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 \csc.exe" /t:library /utf8output 等等...等等... 现在的问题是为什么 VS 和 ASP.Net 似乎都使用了错误的编译器版本?
  • 好吧,好消息是我在 3.5 中编译了网站项目。最后删除所有引用,注释掉 3.5 代码,将目标更改为 2.0,构建,更改回 3.5,取消注释代码,添加引用,然后使用“var”构建良好。去图...
  • 刚刚遇到一个页面,该页面讨论了添加此 web.config 代码以告诉网站使用 c# v3 编译器进行构建。一定是在我摆弄的时候添加的。 跨度>
  • 很高兴我至少可以为您指明正确的方向。您应该在答案中做出所有这些并接受它。
猜你喜欢
  • 2013-12-27
  • 1970-01-01
  • 1970-01-01
  • 2016-03-12
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 1970-01-01
相关资源
最近更新 更多