Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Wednesday, March 07, 2012

Binding Data from Tasks List To SpgridView using Server Side Code without DataSet

 protected void Page_Load(object sender, EventArgs e)
        {
            sgvTasks.DataSource = getData();
            sgvTasks.DataBind();
        }

        public class TasksData
        {
            public string Title { get; set; }
            public string TaskURL { get; set; }
            public string Status { get; set; }
            public string Priority { get; set; }
        }

        public List<TasksData> getData()
        {

            List<TasksData> MyTasksData = new List<TasksData>();
            using (SPSite site = new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb objWeb = site.OpenWeb())
                {
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
                    SPList lstTasks = objWeb.Lists["Tasks"];
                    SPListItemCollection myColl = lstTasks.GetItems(sQuery);
                    if (myColl.Count > 0)
                    {
                        foreach (SPListItem item in myColl)
                        {
                            MyTasksData.Add(new TasksData()
                            {
                                Title = item["Title"].ToString(),
                                Status = (item["Status"] == null) ? "Data UnAvailable" : item["Status"].ToString(),
                                Priority = (item["Priority"] == null) ? "Data UnAvailable" : item["Priority"].ToString(),
                                TaskURL = SPContext.Current.Web.Url + "/Lists/Tasks/DispForm.aspx?ID=" + item["ID"].ToString() + "&Source=" + Page.Request.Url
                            });
                        }
                    }
                }
            }
            return MyTasksData;


        }

No comments:

Post a Comment