Bind SPList to RadGrid

4 posts, 0 answers
  1. Shiri
    Shiri avatar
    2 posts
    Member since:
    Jun 2009

    Posted 24 Jun 2009 Link to this post

    Hello,

    We have tried to bind SPList to the RadGrid using two strategies that we saw in the examples and forum.
    1.To use the method SPList.GetDataTable() and to bind the dataTable to the RadGrid. The problem with this option that GetDataTable() contains only String and Int32 types, no Boolean and other types of SPField.
    2. The second strategy was to bind SPListItemCollection of the list (we tryed to return spesific columns and all items).
    The problem here is RadGrid does not display all columns that are in SPListItemCollection, particulary it does not display the columns that were added manually from sharepoint UI. We work on server side, we also tried autogenerate columns and custom generate, in both cases there is no results in RadGrid.

    Can you please sent us some example of how to bind SPListItemCollection to RadGrid or any other way to bind SPList to RadGrid.

    Thank you,
    Shiri.
  2. Bruno
    Bruno avatar
    105 posts
    Member since:
    Mar 2008

    Posted 26 Jun 2009 Link to this post

    I think that the problem lies in the fact that SPField does not provide a way to directly access its value through a property, this is why custom list fields are not shown event if they are stated explicitly by adding grid columns.

    As a workaround you may try converting the SpList to a regular collection by iterating it and than feed the RadGrid control.
    Another possible solution will be to use GetDataTable method and use Template column which to manually populate.

    --Bruno

  3. Shiri
    Shiri avatar
    2 posts
    Member since:
    Jun 2009

    Posted 28 Jun 2009 Link to this post

    Thank you for your reply,
    We havn't understood how to use the template columns solution in our case.
    We'll be very gratefull if you could send us a code sample, of how to use your given solution.

    In addition, we tried the solution that creates a collection with SPListItems but with no success.

    Regards,
    shiri
  4. Bruno
    Bruno avatar
    105 posts
    Member since:
    Mar 2008

    Posted 30 Jun 2009 Link to this post

    you may try creating a template column with template similar to the following:

      class MyTemplate: ITemplate
            {
                string _fieldName;
                CheckBox _checkbox;

                public MyTemplate(string fieldName)
                {
                    _fieldName = fieldName;
                }           

                public void InstantiateIn(Control container)
                {
                    _checkbox = new CheckBox();
                    _checkbox.ID = "CheckBox1";
                    container.Controls.Add(_checkbox);
                    container.DataBinding += new EventHandler(container_DataBinding);
                }

                void container_DataBinding(object sender, EventArgs e)
                {
                    GridDataItem gridDataItem = ((GridDataItem)((Control)sender).NamingContainer);
                    string value = DataBinder.Eval(((GridDataItem)((Control)sender).NamingContainer).DataItem, fieldName).ToString();
                    _checkbox.Checked = value == "1" ? true : false;
                }
            }

    Note that in this case the GetDataTable method is used to populate to RadGrid control.

    --Bruno
Back to Top