using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using Microsoft.SharePoint;
namespace MyWorkFLowTask.MyTaskEvent
{
public partial class MyTaskEventUserControl
: UserControl
{
#region
Declarations
DataTable myTasks = new
DataTable();
public class Task
{
public string Subject
{ get; set; }
public string
ActionItem { get; set;
}
public string AssignedTo
{ get; set; }
public int MinuteID {
get; set; }
public int TaskID { get; set; }
public DateTime
DueDate { get; set;
}
public int Perc { get; set; }
}
#endregion
/// <summary>
/// Refresh Function
/// </summary>
public int Refresh
{
get { return 1; }
}
/// <summary>
/// Custom Method to check
Date null value
/// </summary>
/// <param name="myValue"></param>
/// <returns></returns>
public string
checkDateNull(object myValue)
{
string value = Convert.ToString(myValue);
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}
else
{
return Convert.ToDateTime(myValue).ToString("MM/dd/yyyy");
}
}
/// <summary>
/// Custom Method to check
object null value
/// </summary>
/// <param name="myValue"></param>
/// <returns></returns>
public string
checkNull(object myValue)
{
string value = Convert.ToString(myValue);
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}
else
{
return myValue.ToString();
}
}
/// <summary>
/// Page Load Event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void
Page_Load(object sender, EventArgs e)
{
SPWeb web = SPContext.Current.Web;
try
{
if (!Page.IsPostBack)
{
BindMyTasks();
}
}
catch (Exception
exp)
{
//
}
finally
{
//
}
}
/// <summary>
/// Custom Method to get Tasks from SP List
/// </summary>
/// <returns></returns>
DataTable GetMyTasks()
{
DataTable itmcoll = new
DataTable();
DataTable dt = new
DataTable();
try
{
SPWeb web = SPContext.Current.Web;
SPUser user = web.CurrentUser;
SPList task = web.Lists.TryGetList("Workflow Tasks");
SPListItemCollection items =
task.Items;
itmcoll = items.GetDataTable();
var query = from
c in itmcoll.AsEnumerable()
// where Convert.ToString(c["AssignedTo"]) ==
SPContext.Current.Web.CurrentUser.Name
select c;
dt = query.CopyToDataTable();
if (dt != null)
{
foreach (DataRow
item in dt.Rows)
{
item["PercentComplete"] = Convert.ToInt32(Convert.ToDecimal(item["PercentComplete"]) * 100);
string Title = Convert.ToString(item["Title"]);
if (Title.Length > 22)
{
Title =
Title.Substring(0, 22) + "...";
item["Title"] = Title;
}
}
}
}
catch (Exception
exp)
{
//
}
finally
{
//
}
return dt;
}
/// <summary>
/// Custom Method to bind
tasks to the grid
/// </summary>
protected void
BindMyTasks()
{
try
{
spgrdMyTasks.PagerTemplate = null;
myTasks = GetMyTasks();
if (ViewState["SortExpression"]
!= null && !string.IsNullOrEmpty(Convert.ToString(ViewState["SortExpression"])))
{
myTasks = SortTable(myTasks);
}
spgrdMyTasks.DataSource = myTasks;
spgrdMyTasks.DataBind();
if (myTasks != null
&& myTasks.Rows.Count > 0)
{
lblPageCount.Text = Convert.ToString(myTasks.Rows.Count)
+ " Records";
}
else
{
lblPageCount.Text = "No Records
Found";
}
}
catch (Exception
exp)
{
//
}
finally
{
//
}
}
/// <summary>
/// Custom method to sort
Grid Data
/// </summary>
/// <param name="dtGridData"></param>
/// <returns></returns>
DataTable SortTable(DataTable
dtGridData)
{
try
{
DataView dvGridDataView =
dtGridData.DefaultView;
string strSortOrder = string.Empty;
if (gridViewSortDirection == SortDirection.Ascending)
{
strSortOrder = "asc";
}
else
{
gridViewSortDirection = SortDirection.Ascending;
strSortOrder = "desc";
}
dvGridDataView.Sort = ViewState["SortExpression"]
+ " " + strSortOrder;
dtGridData = dvGridDataView.ToTable();
}
catch (Exception
exp)
{
//
}
finally
{
//
}
return dtGridData;
}
/// <summary>
/// Page Index Changing
Event for the Grid
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void
spgrdMyTasks_PageIndexChanging(object sender,
System.Web.UI.WebControls.GridViewPageEventArgs
e)
{
try
{
spgrdMyTasks.PageIndex = e.NewPageIndex;
BindMyTasks();
}
catch (Exception
exp)
{
//
}
finally
{
//
}
}
/// <summary>
/// Row Created Event for
the Grid
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void
spgrdMyTasks_RowCreated(object sender,
System.Web.UI.WebControls.GridViewRowEventArgs
e)
{
try
{
if (e.Row.RowType == DataControlRowType.Header)
{
int sortColumnIndex =
getSortColumnIndex();
if (sortColumnIndex != -1)
{
addSortImage(sortColumnIndex, e.Row);
}
}
}
catch (Exception
exp)
{
//
}
finally
{
//
}
}
/// <summary>
/// Grid Data Sorting Event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void
spgrdMyTasks_Sorting(object sender,
System.Web.UI.WebControls.GridViewSortEventArgs
e)
{
try
{
DataTable dtGridData = GetMyTasks();
ViewState["SortExpression"]
= e.SortExpression;
DataView dvGridDataView =
dtGridData.DefaultView;
string strSortOrder = "";
if (gridViewSortDirection == SortDirection.Ascending)
{
gridViewSortDirection = SortDirection.Descending;
strSortOrder = "desc";
}
else
{
gridViewSortDirection = SortDirection.Ascending;
strSortOrder = "asc";
}
dvGridDataView.Sort = ViewState["SortExpression"]
+ " " + strSortOrder;
dtGridData = dvGridDataView.ToTable();
spgrdMyTasks.DataSource = dtGridData;
spgrdMyTasks.DataBind();
}
catch (Exception exp)
{
//
}
finally
{
//
}
}
/// <summary>
/// Gets the Column Index
of the Columns based on which the Sorting should be done.
/// </summary>
/// <returns></returns>
private int
getSortColumnIndex()
{
try
{
foreach (DataControlField
field in spgrdMyTasks.Columns)
{
if (field.SortExpression == (string)ViewState["SortExpression"])
{
return spgrdMyTasks.Columns.IndexOf(field);
}
}
}
catch (Exception
exp)
{
//
}
finally
{
//
}
return -1;
}
/// <summary>
/// This method swaps the
sort direction of the Grid based upon the current sorted direction.
/// If the current sorted
order is Ascending then the new sort direction will be Descending
/// or Vice versa.
/// </summary>
private SortDirection
gridViewSortDirection
{
get
{
if (ViewState["SortOrder"]
== null)
{
ViewState["SortOrder"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["SortOrder"];
}
set
{
ViewState["SortOrder"] = value;
}
}
/// <summary>
/// Adds Sort Image to
Sorted Column.
/// </summary>
/// <param name="columnIndex"></param>
/// <param name="headerRow"></param>
private void
addSortImage(int columnIndex, GridViewRow headerRow)
{
try
{
// Create the sorting image based on the sort
direction.
Image sortImage = new Image();
if (gridViewSortDirection == SortDirection.Ascending)
{
sortImage.ImageUrl = "~/_layouts/15/Branding/images/up_arrow.png";
sortImage.AlternateText = "Ascending Order";
}
else
{
sortImage.ImageUrl = "~/_layouts/15/Branding/images/Down_arrow.png";
sortImage.AlternateText = "Descending
Order";
}
// Add the image to the appropriate header
cell.
headerRow.Cells[columnIndex].Controls.Add(sortImage);
}
catch (Exception
exp)
{
//
}
finally
{
//
}
}
protected void
spgrdMyTasks_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
int count =
e.Row.Cells[0].Controls[0].Controls[0].Controls.Count;
if (count == 1)
{
if (spgrdMyTasks.PageIndex == 0)
{
TableCell next = (TableCell)e.Row.Cells[0].Controls[0].Controls[0].Controls[0];
next.ToolTip = "Next";
}
else
{
TableCell prev = (TableCell)e.Row.Cells[0].Controls[0].Controls[0].Controls[0];
prev.ToolTip = "Previous";
}
}
else if
(count == 2)
{
TableCell prev = (TableCell)e.Row.Cells[0].Controls[0].Controls[0].Controls[0];
prev.ToolTip = "Previous";
TableCell next = (TableCell)e.Row.Cells[0].Controls[0].Controls[0].Controls[1];
next.ToolTip = "Next";
}
}
}
}
}