简体   繁体   English

将数据表DataTable加载到SPGridview C#

[英]Loading data form DataTable to SPGridview C#

I'm making some webparts for myself and I want to populate SPGridview with some data. 我正在为自己制作一些Web部件,并想用一些数据填充SPGridview。

DataTable table = /*some data*/;
SPGridview grid = new SPGridview();

// setting up SPGridview properties 

grid.DataSource = table;
grid.DataBind();

When I did that with GirdView it worked just fine. 当我使用GirdView做到这一点时,它工作得很好。 But when I tried to do that with SPGridview my webpart shows just blank page. 但是,当我尝试使用SPGridview执行此操作时,我的Web部件仅显示空白页。 I suppose that SPGridview need a little more from my side and if you could give me tip how to do it. 我想SPGridview需要我的帮助,如果您能给我一些提示,请告诉我如何做。

        SPGridView myGridView = new SPGridView();
        myGridView.Enabled = true;

        myGridView.DataSource = table;
        myGridView.DataBind();


       this.Controls.Add(myGridView);

Try this code............ 试试这个代码............

For some reason Microsoft disabled AutoGenerateColumns property and I don't know how to bypass without using a loop. 由于某种原因,Microsoft禁用了AutoGenerateColumns属性,而且我不知道如何在不使用循环的情况下绕过。 Here is a working code: 这是一个工作代码:

        SPGridView _gridview = new SPGridView();
        DataTable table = /*some data*/;

        _gridview.Enabled = true;
        _gridview.AutoGenerateColumns = false;
        _gridview.Visible = true;

        foreach(DataColumn col in table.Columns)
        {
            SPBoundField field = new SPBoundField();
            field.DataField = col.ColumnName;
            _gridview.Columns.Add(field);
        }

        _gridview.DataSource = table;
        _gridview.DataBind();

        this.Controls.Add(_gridview);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM