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.

Sunday, February 10, 2013

Binding Date along with Time in GridView using SharePoint Data




Note:- To Get Time along with Date Please follow the below steps. For Asp.Net you no need to do anything.
1.       Go to List Settings in your List.
2.       Click on your Date Column
3.       Now in this page go to Date & Time Format:
4.       In this by default Date Radio Button is clicked.
5.       Select Date & Time Radio Button and click on OK.
Ascx
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DateFormatUserControl.ascx.cs" Inherits="DateFormat.DateFormat.DateFormatUserControl" %>
<asp:GridView ID="dgvTasks" runat="server" AutoGenerateColumns="False"
    BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
    CellPadding="3" EnableModelValidation="True" GridLines="Vertical" Width="100%">
    <AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:BoundField HeaderText="Task Name" DataField="Title" />
<asp:BoundField HeaderText="Status" DataField="Status" />
<asp:BoundField HeaderText="Priority" DataField="Priority" />
<asp:BoundField HeaderText="Start Date" DataField="StartDate" DataFormatString="{0:G}" />
<asp:TemplateField HeaderText="Due Date">
<ItemTemplate>
<asp:Label id="lblDueDate" runat="server" text='<%# Eval("DueDate", "{0:G}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
</asp:GridView>

Ascx.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;

namespace DateFormat.DateFormat
{
    public partial class DateFormatUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getTasks();
            }
        }

        public void getTasks()
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWeb currentWeb = SPContext.Current.Web;
                    SPList lst = currentWeb.Lists["Tasks"];
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name=\"ID\" Ascending=\"False\" /></OrderBy>";
                    SPListItemCollection myColl = lst.GetItems(sQuery);
                    if (myColl.Count > 0)
                    {
                        dgvTasks.DataSource = myColl.GetDataTable();
                        dgvTasks.DataBind();
                    }
                });
            }
            catch (Exception Ex)
            {
                Response.Write(Ex.ToString());
            }
        }
    }
}
Followed this link:-

No comments:

Post a Comment