开发者

How to show only month and year of date in DataColumn of Datatable?

I am populating one DataGridView in C# windows application. I have one DataTable in which I am displaying dates which I retrieve from database. My pr开发者_C百科oblem is, dates that I am getting from database includes time information also. For example: Jan 2003 12:00:00 AM.

I want to show only the year and month (Jan 2003) part of that date in cells of datatable. I tried to change the culture information of current thread, but it didnt worked. Is there any other way to achieve this?

I am using MySQLas my database and there I have set datatype of this column as DATE, but I am still getting this time information. Can anyone explain this behavior of MySQL as well?


Set the Format property on your column to the Month/Year format specifier:

// Set the Format property on the "Last Prepared" column to cause
// the DateTime to be formatted as "Month, Year".
dataGridView1.Columns["Last Prepared"].DefaultCellStyle.Format = "y";


If you prefer to do this at the SQL level, you can do this in your select;

select convert(varchar(max),getdate(),107) as thedate

I'd always recommend not abstracting the data and letting the datagridview doing the cosmetics however.


Thanks everyone. I solved my problem by changing my query. I used date_format() function of MySql which gave me exact format . Here is the link which gives more information about this function.

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜