试图从Linq绑定GridView

时间:2013-05-02 21:40:19

标签: c# asp.net xml linq gridview

所以我有一个从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。

0 个答案:

没有答案