Usually when we try to find some out of use column in default grid provided by .net, we are not able to do it. We require inheriting base class and then we need to put information inside of it.
Say for example, you require adding datetime picker column to your windows form grid, if you check all supported column types in grid, you won’t find such in it. We require writing custom code for it and then adding that columns in our grid control.
Here is some code for creating date time picker column, please make sure that you can add this code in separate component code and then you can access this column in whole project when ever needs.
using System;
using System.Windows.Forms;
publicclass CalendarColumn : DataGridViewColumn
{
public CalendarColumn() : base(new CalendarCell())
{
}
publicoverride DataGridViewCell CellTemplate
{
get
{
returnbase.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a CalendarCell.
if (value != null&&
!value.GetType().IsAssignableFrom(typeof(CalendarCell)))
{
thrownew InvalidCastException("Must be a CalendarCell");
}
base.CellTemplate = value;
}
}
}
publicclass CalendarCell : DataGridViewTextBoxCell
{
public CalendarCell()
: base()
{
// Use the short date format.
this.Style.Format = "d";
}
publicoverridevoid InitializeEditingControl(int rowIndex, object
initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
// Set the value of the editing control to the current cell value.
base.InitializeEditingControl(rowIndex, initialFormattedValue,
dataGridViewCellStyle);
CalendarEditingControl ctl =
DataGridView.EditingControl as CalendarEditingControl;
// Use the default row value when Value property is null.
if (this.Value == null)
{
ctl.Value = (DateTime)this.DefaultNewRowValue;
}
else
{
ctl.Value = (DateTime)this.Value;
}
}
publicoverride Type EditType
{
get
{
// Return the type of the editing control that CalendarCell uses.
returntypeof(CalendarEditingControl);
}
}
publicoverride Type ValueType
{
get
{
// Return the type of the value that CalendarCell contains.
returntypeof(DateTime);
}
}
publicoverrideobject DefaultNewRowValue
{
get
{
// Use the current date and time as the default value.
return DateTime.Now;
}
}
}
class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
{
DataGridView dataGridView;
privatebool valueChanged = false;
int rowIndex;
public CalendarEditingControl()
{
this.Format = DateTimePickerFormat.Short;
}
// Implements the IDataGridViewEditingControl.EditingControlFormattedValue
// property.
publicobject EditingControlFormattedValue
{
get
{
returnthis.Value.ToShortDateString();
}
set
{
if (value is String)
{
try
{
// This will throw an exception of the string is
// null, empty, or not in the format of a date.
this.Value = DateTime.Parse((String)value);
}
catch
{
// In the case of an exception, just use the
// default value so we're not left with a null
// value.
this.Value = DateTime.Now;
}
}
}
}
// Implements the
// IDataGridViewEditingControl.GetEditingControlFormattedValue method.
publicobject GetEditingControlFormattedValue(
DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
// Implements the
// IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
publicvoid ApplyCellStyleToEditingControl(
DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
}
// Implements the IDataGridViewEditingControl.EditingControlRowIndex
// property.
publicint EditingControlRowIndex
{
get
{
return rowIndex;
}
set
{
rowIndex = value;
}
}
// Implements the IDataGridViewEditingControl.EditingControlWantsInputKey
// method.
publicbool EditingControlWantsInputKey(
Keys key, bool dataGridViewWantsInputKey)
{
// Let the DateTimePicker handle the keys listed.
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
returntrue;
default:
return !dataGridViewWantsInputKey;
}
}
// Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit
// method.
publicvoid PrepareEditingControlForEdit(bool selectAll)
{
// No preparation needs to be done.
}
// Implements the IDataGridViewEditingControl
// .RepositionEditingControlOnValueChange property.
publicbool RepositionEditingControlOnValueChange
{
get
{
returnfalse;
}
}
// Implements the IDataGridViewEditingControl
// .EditingControlDataGridView property.
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}
// Implements the IDataGridViewEditingControl
// .EditingControlValueChanged property.
publicbool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
valueChanged = value;
}
}
// Implements the IDataGridViewEditingControl
// .EditingPanelCursor property.
public Cursor EditingPanelCursor
{
get
{
returnbase.Cursor;
}
}
protectedoverridevoid OnValueChanged(EventArgs eventargs)
{
// Notify the DataGridView that the contents of the cell
// have changed.
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
}
}