三列合并到datagridview中的一列 [英] three columns merged to one one column in datagridview
本文介绍了三列合并到datagridview中的一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的datagridview中有3列这样的格式
In My datagridview having 3 columns like this format
date month year
1 2 2012
2 3 2012
3 1 2012
i想要输出像这个
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
推荐答案
在SQL Server端:
SQL Server提供日期付款方式 [ ^ ]函数。
对于以前版本的MS SQL Sever,您需要使用日期和时间函数 [ ^ ]。
示例:
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:
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]
在C#中代码:
In C# code:
DateTime dt = New DateTime (2012, 12, 1, 0, 0, 0)
设置datetime变量时,需要使用自定义日期和时间格式字符串 [ ^ ]。
假设datagridview绑定到数据表并且date,month,year列是整数类型,那么您可以使用一个讨厌的列表达式将Date_Format列添加到数据表中。 :)
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. :)
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);
这篇关于三列合并到datagridview中的一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文