我有一个父网格视图(usertransactionridview),我与这个linq查询的输出绑定,输出看起来像这样:http://postimage.org/image/1vtpbagck/:
var recordList = (from CustomersRecord custsRecord in customerRecordContainer orderby custsRecord.DateOfTransaction descending group custsRecord
by new {custsRecord.InvoiceNumber, custsRecord.DateOfTransaction} into intermediateResult
select new {InvoiceNumber = intermediateResult.Key.InvoiceNumber, DateOfTransaction = intermediateResult.Key.DateOfTransaction, TotalAmount = intermediateResult.Sum(custsRecord => custsRecord.TotalAmount)}).ToList();
UserTransactionGridView.DataSource = recordList;
UserTransactionGridView.DataBind();
现在,当数据被绑定到父网格视图中的每一行时,我正在创建一个子网格视图(gridview2),这取决于每一行发票号。我的意思是,根据每一行的发票号码,我从数据库中获取软件标题,并以这种方式将其绑定到子网格视图。我的意图是得到如下的输出:http://postimage.org/image/2lri4f544/。为了达到这个目的,我做了以下操作:
protected void UserTransactionGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gridView2 = (GridView)e.Row.FindControl("gridView2");
System.Data.DataRowView dr = (System.Data.DataRowView)e.Row.DataItem;
gridView2.AutoGenerateColumns = true;
String x = dr[1].ToString();
softwareTitlesList = SoftwareListRetrieve();
ArrayList titles = new ArrayList();
foreach (SoftwareTitles softwareTitle in softwareTitlesList)
{
if (softwareTitle.InvoiceNumber.Contains(x))
titles.Add(softwareTitle.SoftwareTitle);
}
gridView2.DataSource = titles;
gridView2.DataBind();
softwareTitlesList.Clear();
}
}
但是我得到的错误如下:
Unable to cast object of type '<>f__AnonymousType3`3[System.String,System.DateTime,System.Decimal]' to type 'System.Data.DataRowView'.
您正试图将e.Row.DateItem
转换为DataRowView
,我认为这行不通。请尝试以下示例以供参考。在那篇文章的最后一段代码中,做了类似的事情。