IDataGridViewEditingControl Gränssnitt
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Definierar vanliga funktioner för kontroller som finns i celler i en DataGridView.
public interface class IDataGridViewEditingControl
public interface IDataGridViewEditingControl
type IDataGridViewEditingControl = interface
Public Interface IDataGridViewEditingControl
- Härledda
Exempel
Följande kodexempel innehåller en implementering av det här gränssnittet som härleds från DateTimePicker. Det här exemplet är en del av ett större exempel i How to: Host Controls in Windows Forms DataGridView Cells.
class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;
public CalendarEditingControl()
{
this.Format = DateTimePickerFormat.Short;
}
// Implements the IDataGridViewEditingControl.EditingControlFormattedValue
// property.
public object EditingControlFormattedValue
{
get
{
return this.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.
public object GetEditingControlFormattedValue(
DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
// Implements the
// IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
public void ApplyCellStyleToEditingControl(
DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
}
// Implements the IDataGridViewEditingControl.EditingControlRowIndex
// property.
public int EditingControlRowIndex
{
get
{
return rowIndex;
}
set
{
rowIndex = value;
}
}
// Implements the IDataGridViewEditingControl.EditingControlWantsInputKey
// method.
public bool 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:
return true;
default:
return !dataGridViewWantsInputKey;
}
}
// Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit
// method.
public void PrepareEditingControlForEdit(bool selectAll)
{
// No preparation needs to be done.
}
// Implements the IDataGridViewEditingControl
// .RepositionEditingControlOnValueChange property.
public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
// Implements the IDataGridViewEditingControl
// .EditingControlDataGridView property.
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}
// Implements the IDataGridViewEditingControl
// .EditingControlValueChanged property.
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
valueChanged = value;
}
}
// Implements the IDataGridViewEditingControl
// .EditingPanelCursor property.
public Cursor EditingPanelCursor
{
get
{
return base.Cursor;
}
}
protected override void OnValueChanged(EventArgs eventargs)
{
// Notify the DataGridView that the contents of the cell
// have changed.
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
}
}
Class CalendarEditingControl
Inherits DateTimePicker
Implements IDataGridViewEditingControl
Private dataGridViewControl As DataGridView
Private valueIsChanged As Boolean = False
Private rowIndexNum As Integer
Public Sub New()
Me.Format = DateTimePickerFormat.Short
End Sub
Public Property EditingControlFormattedValue() As Object _
Implements IDataGridViewEditingControl.EditingControlFormattedValue
Get
Return Me.Value.ToShortDateString()
End Get
Set(ByVal value As Object)
Try
' This will throw an exception of the string is
' null, empty, or not in the format of a date.
Me.Value = DateTime.Parse(CStr(value))
Catch
' In the case of an exception, just use the default
' value so we're not left with a null value.
Me.Value = DateTime.Now
End Try
End Set
End Property
Public Function GetEditingControlFormattedValue(ByVal context _
As DataGridViewDataErrorContexts) As Object _
Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
Return Me.Value.ToShortDateString()
End Function
Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As _
DataGridViewCellStyle) _
Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
Me.Font = dataGridViewCellStyle.Font
Me.CalendarForeColor = dataGridViewCellStyle.ForeColor
Me.CalendarMonthBackground = dataGridViewCellStyle.BackColor
End Sub
Public Property EditingControlRowIndex() As Integer _
Implements IDataGridViewEditingControl.EditingControlRowIndex
Get
Return rowIndexNum
End Get
Set(ByVal value As Integer)
rowIndexNum = value
End Set
End Property
Public Function EditingControlWantsInputKey(ByVal key As Keys, _
ByVal dataGridViewWantsInputKey As Boolean) As Boolean _
Implements IDataGridViewEditingControl.EditingControlWantsInputKey
' Let the DateTimePicker handle the keys listed.
Select Case key And Keys.KeyCode
Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, _
Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp
Return True
Case Else
Return Not dataGridViewWantsInputKey
End Select
End Function
Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
' No preparation needs to be done.
End Sub
Public ReadOnly Property RepositionEditingControlOnValueChange() _
As Boolean Implements _
IDataGridViewEditingControl.RepositionEditingControlOnValueChange
Get
Return False
End Get
End Property
Public Property EditingControlDataGridView() As DataGridView _
Implements IDataGridViewEditingControl.EditingControlDataGridView
Get
Return dataGridViewControl
End Get
Set(ByVal value As DataGridView)
dataGridViewControl = value
End Set
End Property
Public Property EditingControlValueChanged() As Boolean _
Implements IDataGridViewEditingControl.EditingControlValueChanged
Get
Return valueIsChanged
End Get
Set(ByVal value As Boolean)
valueIsChanged = value
End Set
End Property
Public ReadOnly Property EditingControlCursor() As Cursor _
Implements IDataGridViewEditingControl.EditingPanelCursor
Get
Return MyBase.Cursor
End Get
End Property
Protected Overrides Sub OnValueChanged(ByVal eventargs As EventArgs)
' Notify the DataGridView that the contents of the cell have changed.
valueIsChanged = True
Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
MyBase.OnValueChanged(eventargs)
End Sub
End Class
Kommentarer
Det här gränssnittet implementeras av kontroller, till exempel och , som DataGridViewComboBoxEditingControl hanteras av motsvarande DataGridViewTextBoxEditingControl celler, till exempel DataGridView och DataGridViewComboBoxCell, när de är i redigeringsläge.DataGridViewTextBoxCell
Celltyper som kan vara värdar för redigeringskontroller anger deras EditType egenskap till en Type som representerar redigeringskontrolltypen. När cellen går in i redigeringsläge utförs följande steg:
Kontrollen DataGridView skapar en instans av redigeringskontrolltypen.
Kontrollen DataGridView anropar cellmetoden InitializeEditingControl . Du kan åsidosätta den här metoden för att överföra cellvärdet till redigeringskontrollen.
Kontrollen DataGridView anropar redigeringskontrollmetoden ApplyCellStyleToEditingControl och skickar cellens aktuella formatmall. Du kan implementera den här metoden för att initiera utseendet på redigeringskontrollen så att den matchar cellens utseende.
Kontrollen DataGridView anropar redigeringskontrollmetoden PrepareEditingControlForEdit . Du kan implementera den här metoden för att göra slutgiltiga justeringar i redigeringskontrollen, till exempel genom att välja kontrollvärdet.
Mer information om hur du implementerar IDataGridViewEditingControl finns i How to: Host Controls in Windows Forms DataGridView Cells.
Celltyper som DataGridViewCheckBoxCell det tillhandahåller ett användargränssnitt (UI) för att ange värden utan att vara värd för en redigeringskontroll implementerar IDataGridViewEditingCell gränssnittet. Användargränssnittet i det här fallet visas oavsett om cellen är i redigeringsläge.
Andra celltyper, till exempel DataGridViewButtonCell, tillhandahåller ett användargränssnitt men lagrar inte användardefinierade värden. I det här fallet implementerar IDataGridViewEditingCell eller är celltypen inte värd för en redigeringskontroll.
Egenskaper
| Name | Description |
|---|---|
| EditingControlDataGridView |
Hämtar eller anger DataGridView som innehåller cellen. |
| EditingControlFormattedValue |
Hämtar eller anger det formaterade värdet för cellen som ändras av redigeraren. |
| EditingControlRowIndex |
Hämtar eller anger indexet för värdcellens överordnade rad. |
| EditingControlValueChanged |
Hämtar eller anger ett värde som anger om värdet för redigeringskontrollen skiljer sig från värdet för värdcellen. |
| EditingPanelCursor |
Hämtar markören som används när muspekaren EditingPanel är över men inte över redigeringskontrollen. |
| RepositionEditingControlOnValueChange |
Hämtar eller anger ett värde som anger om cellinnehållet behöver flyttas när värdet ändras. |
Metoder
| Name | Description |
|---|---|
| ApplyCellStyleToEditingControl(DataGridViewCellStyle) |
Ändrar kontrollens användargränssnitt så att det överensstämmer med det angivna cellformatet. |
| EditingControlWantsInputKey(Keys, Boolean) |
Avgör om den angivna nyckeln är en vanlig indatanyckel som redigeringskontrollen ska bearbeta eller en särskild nyckel som ska bearbetas DataGridView . |
| GetEditingControlFormattedValue(DataGridViewDataErrorContexts) |
Hämtar cellens formaterade värde. |
| PrepareEditingControlForEdit(Boolean) |
Förbereder den markerade cellen för redigering. |