gpt4 book ai didi

c# - LINQ 数据绑定(bind)到 GridView 和 RowDataBound 的问题

转载 作者:太空宇宙 更新时间:2023-11-03 16:55:30 25 4
gpt4 key购买 nike

大家好

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

The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)

这个有趣的部分是,如果我只是尝试在同一文件的其他任何地方输入 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

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

最佳答案

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

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

关于c# - LINQ 数据绑定(bind)到 GridView 和 RowDataBound 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2213065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com