开发者

Fill DataTable having one column as DateTime value and bind with DataGridView

I have data from an excel file which contains one column as DateTime with a specific format. Now I retrieved data from the file using dataadapter and fill the datatable.

How can I change the DateTime column format into specific culture before binding dat开发者_StackOverflowatable with datagridview?


You could use the DefaultCellStyle property of the DateTime column of your DataGridView to format and display your DateTime values in a culture specific format. Here is a small example:

DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("dayofbirth", typeof(DateTime));

// Fill your data table
...

// Bind your data table against the grid view.    
dataGridView1.DataSource = dt;

// Set format styles for your date columns (after binding)
CultureInfo ci = CultureInfo.CreateSpecificCulture("en-US");
dataGridView1.Columns[1].DefaultCellStyle.FormatProvider = ci;
dataGridView1.Columns[1].DefaultCellStyle.Format = ci.DateTimeFormat.LongDatePattern;

Hope, this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜