所以我有一个从 XML 文件填充的 DataSet,我试图使用 LINQ 填充 GridView。但是 GridView 呈现为空。这是代码隐藏...
protected void Page_Load(object sender, EventArgs e)
{
LinqSamples samples = new LinqSamples(gvNumbers, this.Page);
samples.DataSetLinq40();
}
private class LinqSamples
{
private DataSet testDS;
private GridView _gv;
private Page _pg;
public LinqSamples(GridView gv, Page pg)
{
testDS = TestHelper.CreateTestDataSet(pg);
_gv = gv;
_pg = pg;
}
public void DataSetLinq40()
{
var numbers = testDS.Tables["Numbers"].AsEnumerable();
//var numberGroups = from n in numbers
// group n by n.Field<int>("number") % 5 into g
// select new { Remainder = g.Key, Numbers = g };
//var justNums = from n in numbers
// select n;
_gv.DataSource = justNums;
_gv.DataBind();
}
}
internal static DataSet CreateTestDataSet(Page pg)
{
DataSet ds = new DataSet();
// Customers Table
ds.Tables.Add(CreateNumbersTable());
ds.AcceptChanges();
return ds;
}
private static DataTable CreateNumbersTable()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
DataTable table = new DataTable("Numbers");
table.Columns.Add("number", typeof(int));
foreach (int n in numbers)
{
table.Rows.Add(new object[] { n });
}
return table;
}
...和 .aspx 页面...
<asp:GridView ID="gvNumbers" runat="server" AutoGenerateColumns="false">
</asp:GridView>
numberGroups 和 justNums 都不起作用,都导致一个空的 GridView。