There is actually a better way than creating a custom DataGridView column. What I'm doing now for my application is when the TIMESTAMP column of my DataGridView is entered, I set the DateTimePicker control directly above the cell. When the user pulled out of the cell (thereby confirming his choice), the DateTimePicker Visible parameter is set to False, and the DateTimePicker value will be placed in the cell. By default, the DateTimePicker Visibility parameter is set to False until I need it. I also use this for ComboBox controls on regular cells where the user cannot enter a custom value and must use the list of items on the settings screen. This method is great for falsification. I don’t have code that is easily accessible,but it is less code and easier to support IMHO.
DataGridView Win Forms 2.0
: -
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (selectAllToolStripMenuItem.Checked)
selectAllToolStripMenuItem.Checked = false;
if (dtPicker.Visible)
dtPicker.Visible = false;
if (e.ColumnIndex >= 0)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "Delete")
{
if (adminIsLoggedIn)
{
removeRow(e);
}
else
{
MessageBox.Show("You must be logged in as an Administrator in order to change the facility configuration.", "Delete Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else if (dataGridView1.Columns[e.ColumnIndex].Name == "TIMESTAMP")
{
if (adminIsLoggedIn)
{
setNewCellDate(e);
}
}
.....
}
}
private void setNewCellDate(DataGridViewCellEventArgs e)
{
dtPicker.Size = dataGridView1.CurrentCell.Size;
dtPicker.Top = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Top + dataGridView1.Top;
dtPicker.Left = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Left + dataGridView1.Left;
if (!(object.Equals(Convert.ToString(dataGridView1.CurrentCell.Value), "")))
dtPicker.Value = Convert.ToDateTime(dataGridView1.CurrentCell.Value);
dtPicker.Visible = true;
}
user195488