0

I'm making some webparts for myself and I want to populate SPGridview with some data.

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. But when I tried to do that with SPGridview my webpart shows just blank page. I suppose that SPGridview need a little more from my side and if you could give me tip how to do it.

1
  • P.S. Assume that I don't know structure of data in table. Commented Jul 6, 2013 at 9:52

2 Answers 2

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

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


       this.Controls.Add(myGridView);

Try this code............

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for last line I forgot about that SPGridView it's not auto-generated control and need to be added.
0

For some reason Microsoft disabled AutoGenerateColumns property and I don't know how to bypass without using a loop. 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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.