Setting a default value for a datagridviewtextbox column
-
I have a datagridview and would like one of it's txtBox columns to contain the Current date by default. Which of the datagridview's events should I use to be able to assign to it's EdittedFormatedValue or something?
Live in fragments no longer. Only connect.
-
I have a datagridview and would like one of it's txtBox columns to contain the Current date by default. Which of the datagridview's events should I use to be able to assign to it's EdittedFormatedValue or something?
Live in fragments no longer. Only connect.
There's more than one way to approach this. One way is to set the DataGridViewTextBoxColumn's DefaultValue property to the current date. This means that all rows with an unspecified value in that column will get the current date. One problem is that the date will remain the same even if the date changes while the application is still running (i.e. new rows will still get the no-longer-current date). Another approach is to use the DataGridView's DefaultValuesNeeded event to set the current date, e.g. (if the target DataGridViewTextBoxColumn is named "DateColumn"):
private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
e.Row.Cells["DateColumn"].Value = DateTime.Now.ToShortDateString();
}This affects only new rows; existing rows will not be updated. If you do both, then existing rows with an unspecified value will get the date on which the application began running and new rows will get the current date.
-
There's more than one way to approach this. One way is to set the DataGridViewTextBoxColumn's DefaultValue property to the current date. This means that all rows with an unspecified value in that column will get the current date. One problem is that the date will remain the same even if the date changes while the application is still running (i.e. new rows will still get the no-longer-current date). Another approach is to use the DataGridView's DefaultValuesNeeded event to set the current date, e.g. (if the target DataGridViewTextBoxColumn is named "DateColumn"):
private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
e.Row.Cells["DateColumn"].Value = DateTime.Now.ToShortDateString();
}This affects only new rows; existing rows will not be updated. If you do both, then existing rows with an unspecified value will get the date on which the application began running and new rows will get the current date.
Thanks Lisa
Live in fragments no longer. Only connect.