Global Variables
DataSet dSet = new DataSet();
DataTable dt = new DataTable();
SPGridView sgvTasks;
Render Part
sgvTasks.RenderControl(writer);
Create Child Controls Part
try
{
sgvTasks = new SPGridView();
sgvTasks.ID = "sgvTasks";
sgvTasks.AutoGenerateColumns = false;
sgvTasks.Attributes.Add("RunAt", "Server");
sgvTasks.RowDataBound += new GridViewRowEventHandler(sgvTasks_RowDataBound);
this.Controls.Add(sgvTasks);
HyperLinkField hlnkTitle = new HyperLinkField();
hlnkTitle.HeaderText = "Task Name";
hlnkTitle.DataTextField = "Title";
hlnkTitle.DataNavigateUrlFields = new string[] { "URL" };
hlnkTitle.DataNavigateUrlFormatString = "{0}";
sgvTasks.Columns.Add(hlnkTitle);
BoundField bStatus = new BoundField();
bStatus.HeaderText = "Task Status";
bStatus.DataField = "Status";
sgvTasks.Columns.Add(bStatus);
sgvTasks.DataSource = getData();
sgvTasks.DataBind();
}
catch (Exception ex)
{
strError += ex.ToString();
}
getData() Function
public DataSet getData()
{
try
{
dt.Columns.Add("URL");
dt.Columns.Add("Title");
dt.Columns.Add("Status");
SPWeb currentWeb = SPControl.GetContextWeb(Context);
SPList lst = currentWeb.Lists["Tasks"];
SPQuery sQuery = new SPQuery();
if (Page.Request.Url.ToString().Contains("Group By Sample.aspx"))
{
sQuery.Query = "<OrderBy><FieldRef Name='Status' Ascending='False' /></OrderBy>";
}
else
{
sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
}
SPListItemCollection myColl = lst.GetItems(sQuery);
string strTitle, strUrl, strStatus = string.Empty;
if (myColl.Count > 0)
{
foreach (SPListItem item in myColl)
{
strUrl = currentWeb.Url + "/Lists/Tasks/DispForm.aspx?ID=" + item.ID + "&Source=" + Page.Request.Url;
strTitle = item["Title"].ToString();
if (item["Status"].ToString() != "")
{
strStatus = item["Status"].ToString();
}
DataRow drow = dt.NewRow();
drow["Title"] = strTitle;
drow["URL"] = strUrl;
drow["Status"] = strStatus;
dt.Rows.Add(drow);
}
}
dSet.Tables.Clear();
dSet.Tables.Add(dt);
return dSet;
}
catch (Exception ex)
{
strError += ex.ToString();
return null;
}
}
No comments:
Post a Comment