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.
精彩评论