Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have created entity data model in DAL. In DAL i have created class Department
as shown in below code.
SQL
namespace DAL
{
    [MetadataType(typeof(DepartmentMetaData))]
    public partial class Department
    {
    }

    public class DepartmentMetaData
    {
        [DataType(DataType.Currency)]
        [Range(0, 1000000, ErrorMessage = "Budget must be less than $1,000,000.00")]
        public Decimal Budget { get; set; }

        [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        public DateTime StartDate { get; set; }

    }
}


in presentation layer i binded to gridview like this shown below:

XML
<asp:ObjectDataSource ID="obj" runat="server" TypeName="DAL.SchoolRepository"
        DataObjectTypeName="DAL.Department" SelectMethod="GetDepartments">
   </asp:ObjectDataSource>
   <asp:GridView ID="gv" runat="server" DataSourceID="obj" AutoGenerateColumns="False">
       <Columns>
           <asp:BoundField DataField="DepartmentID" HeaderText="DepartmentID" SortExpression="DepartmentID" Visible="false">
           </asp:BoundField>
           <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"></asp:BoundField>
           <asp:BoundField DataField="Budget" HeaderText="Budget" SortExpression="Budget"></asp:BoundField>
           <asp:BoundField DataField="StartDate" HeaderText="StartDate" SortExpression="StartDate">
           </asp:BoundField>
           <asp:TemplateField HeaderText="Administrator">
               <ItemTemplate>
                   <asp:Label ID="lblLastName" runat="server" Text='<%# Eval("Person.LastName") %>' />
                   <asp:Label ID="lblFirstName" runat="server" Text='<%# Eval("Person.FirstName") %>' />
               </ItemTemplate>
           </asp:TemplateField>
       </Columns>
   </asp:GridView>

in that aspx.cs
i wrote page_init event like this.
C#
protected void Page_Init(object sender, EventArgs e)
       {
           gv.EnableDynamicData(typeof(Department));
       }
   }


here in budget it should show dollar currency symbol and start date format like wrote in DAL department.cs

please help me thank you.
Posted
Updated 19-Mar-12 20:12pm
v2

1 solution

Have the budget and start date columns defined this way -
ASP.NET
<asp:boundfield datafield="Budget" headertext="Budget" sortexpression="Budget" dataformatstring="{0:C}" ></asp:boundfield>
<asp:boundfield datafield="StartDate" headertext="StartDate" sortexpression="StartDate" dataformatstring="{0:d}" >
</asp:boundfield>

IMHO, this is better than having the display formatting on the DAL.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900