pdanan

See also: Other Geeks@INDC

Bind List items to SPGridView

 

SPGridView Bind to List items :

At File *.aspx :

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">

<SharePoint:SPGridView runat="server" ID="gridView"  AutoGenerateColumns="false" /> 

</asp:Content>

 

At File *.aspx.cs :

 

protected void Page_Load(object sender, EventArgs e)

{

    SPWeb myWeb = SPContext.Current.Web;

    SPList myList = myWeb.Lists.TryGetList("myList");

    if (!Page.IsPostBack)

    {

        BindToGrid(myList, gridView);

    }

}

 

private void BindToGrid(SPList myList, SPGridView grdView)

{

    SPListItemCollection results = myList.Items;

    grdView.AutoGenerateColumns = false;

    grdView.DataSource = results.GetDataTable();

    // create the bound fields

    SPBoundField boundField;

    boundField = new SPBoundField();

    boundField.HeaderText = "Title";

    boundField.DataField = "Title";

    grdView.Columns.Add(boundField);

    boundField = new SPBoundField();

    boundField.HeaderText = "Created";

    boundField.DataField = "Created";

    grdView.Columns.Add(boundField);

    grdView.DataBind(); 

}

 

Share this post: | | | |
Posted: Jul 08 2011, 08:55 PM by pdanan | with no comments |
Filed under: ,

Comments

No Comments