Click here to Skip to main content
15,909,051 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

In My datagridview having 3 columns like this format
date  month  year
  1     2    2012
  2     3    2012
  3     1    2012


i want output like this
date  month  year  Date_Format
 1     2     2012  1st February 2012pre
 2     3     2012  2nd March 2012
 3     1     2012  3rd jan 2012
Posted
Updated 30-Apr-13 10:49am
v2
Comments
sri senthil kumar 30-Apr-13 8:09am    
Hope you need to process in c# itself before binding it to gridview or in store procedure you have to do while fetching from db... You want the code?

On SQL Server side:
SQL Server provides DATEFROMPARTS[^] function.
For previous version of MS SQL Sever you need to use Date And Time Functions[^].

Example:
SQL
DECLARE @day INT
DECLARE @month INT
DECLARE @year INT
DECLARE @initDate DATETIME

SET @day = 1
SET @month = 2
SET @year = 2012
SET @initDate = '1900-01-01'

SELECT @day AS [Day], @month AS [Month], @year AS [Year]
SELECT DATEADD(dd, @day -DAY(@initdate), DATEADD(mm, @month-MONTH(@initDate), DATEADD(yy, @year-YEAR(@initDate), @initDate))) AS [Date]



In C# code:
C#
DateTime dt = New DateTime (2012, 12, 1, 0, 0, 0)

When you set datetime variable, you need to format it, using Custom Date and Time Format Strings[^].
 
Share this answer
 
Assuming that the datagridview is bound to a datatable and the date, month, year columns are integer type, then you could add a the Date_Format column to the datatable with one nasty column expression. :)

C#
string expr = "Convert([date], System.String) + IIf([date] > 10 And [date] < 20, 'th', IIf([date] % 10 = 1, 'st', IIf([date] % 10 = 2, 'nd', IIf([date] % 10 = 3, 'rd', 'th')))) + " 
              + "' ' + IIf([month] = 1, 'January', IIf([month] = 2, 'February', IIf([month] = 3, 'March', IIf([month] = 4, 'April', IIf([month] = 5, 'May', IIf([month] = 6, 'June', IIf([month] = 7, 'July', IIf([month] = 8, 'August', IIf([month] = 9, 'September', IIf([month] = 10, 'October', IIf([month] = 11, 'November', IIf([month] = 12, 'December', '')))))))))))) + " 
              + "' ' + Convert([year], System.String)";

yourDataTable.Columns.Add("Date_Format", typeof(string), expr);
 
Share this answer
 
v2

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