Sunday, November 24, 2013

How do i get last created list item in a visual webpart

Hi,


We can do as follows:


1.Add a gridview in your visual web part which is used to display all fields of the last item.



<asp:GridView ID="GridView1" runat="server"></asp:GridView>

2.Bind the list item to the gridview.



SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["YourListName"];
SPQuery query = new SPQuery();
query.RowLimit = 1;
query.Query = "<OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy>";

DataTable items = list.GetItems(query).GetDataTable();
this.GridView1.DataSource = items;
this.GridView1.DataBind();

3.To use DataTable, we need to add the statement below first.



using System.Data;

Best Regards




Dennis Guo

TechNet Community Support



No comments:

Post a Comment