The method you're using for formatting is a valid approach. There are a few issues that may be happening here. Please ensure:
- The underlying data is of type
DateTime
and not type string
. Type string
will not apply the format as expected. (See columns 1 and 2 in example below.)
- The individual
DataGridViewTextBoxCell.Style.Format
isn't being set to something different than your desired format. This would override the column formatting. (See column 3 in example below.)
EXAMPLE
this.dataGridView1.ColumnCount = 4;
this.dataGridView1.RowCount = 1;
this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
DateTime date = DateTime.Now;
this.dataGridView1[0, 0].Value = date;
this.dataGridView1[1, 0].Value = date.ToString();
this.dataGridView1[2, 0].Value = date.ToString("MM/yyyy");
this.dataGridView1[3, 0].Value = date;
this.dataGridView1[3, 0].Style.Format = "MM/yyyy";
this.dataGridView1.Columns[0].DefaultCellStyle.Format = "dd/yyyy";
this.dataGridView1.Columns[1].DefaultCellStyle.Format = "dd/yyyy";
this.dataGridView1.Columns[2].DefaultCellStyle.Format = "dd/yyyy";
this.dataGridView1.Columns[3].DefaultCellStyle.Format = "dd/yyyy";

As you can see from the output:
Columns[0]
contains a DateTime
and correctly formats as "dd/yyyy"
.
Columns[1]
contains a string
and cannot be reformatted to "dd/yyyy"
.
Columns[2]
contains a formatted string
and cannot be reformatted to "dd/yyyy"
.
Columns[3]
contains a DateTime
and formatting is overridden by the cell format "MM/yyyy"
.
To correct these issues, just set your cell value using the DateTime
object and not any string
representation.
In the case where you are getting this data from some outside source and it's already of type string
, you can Parse
it, but note that the missing portions of the DateTime
object will be defaulted and there's really nothing you can do about that without having the original full data:
DateTime date = DateTime.Parse("10/2016");
Console.WriteLine("Output: {0}", date.ToString());
// Output: 10/1/2016 12:00:00 AM
Validation
If validating user input (and losing formatting on edits) is your main concern, consider the following method for validation to cancel an invalid edit:
private void DataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
DateTime parsed;
if (!DateTime.TryParse(e.FormattedValue.ToString(), out parsed))
{
this.dataGridView1.CancelEdit();
}
}
Coupled with this answer for reapplying your format using the DataGridView.CellFormatting
event handler. (Note that this also negates the need to ensure your data types are not string
, but is more costly due to the event being triggered often.)