/// <summary> /// This raises the <see cref="MapIndexToValue"/> event /// </summary> /// <param name="e">The event arguments</param> protected internal virtual void OnMapIndexToValue(MapIndexEventArgs e) { var handler = MapIndexToValue; if(handler != null) handler(this, e); }
/// <summary> /// Gets the image to display in the cell /// </summary> /// <param name="value">The value to be use in determining the image</param> /// <param name="rowIndex">The index of the cell's parent row</param> /// <returns>The image that should be displayed in the cell</returns> protected override object GetCellImage(object value, int rowIndex) { // Use the image from the image list ImageListColumn owner = base.OwningColumn as ImageListColumn; Image cellImage = null; int cellValue = -1; if (owner != null && owner.ImageList != null) { if (value is int) { cellValue = (int)value; } else if (value is short) { cellValue = (short)value; } else if (value is bool) { cellValue = ((bool)value) ? 1 : 0; } // Let the user map the value to an index MapIndexEventArgs mapArgs = new MapIndexEventArgs(owner.Index, rowIndex, value, cellValue); owner.OnMapValueToIndex(mapArgs); cellValue = mapArgs.Index; if (cellValue < 0 || cellValue >= owner.ImageList.Images.Count) { cellValue = owner.NullImageIndex; if (cellValue >= owner.ImageList.Images.Count) { cellValue = -1; } } if (cellValue != -1) { cellImage = owner.ImageList.Images[cellValue]; } else { cellImage = owner.NullImage; } } return(cellImage); }
/// <summary> /// This is overridden to cycle through the image index values when the cell is clicked and the cell is /// editable. /// </summary> /// <param name="e">The event arguments</param> protected override void OnContentClick(DataGridViewCellEventArgs e) { ImageListColumn owner = base.OwningColumn as ImageListColumn; int cellValue = -1; object newValue = base.NewValue; if (!base.ReadOnly && base.DataGridView != null && owner != null && owner.ImageList != null) { if (newValue is int) { cellValue = (int)newValue; } else if (newValue is short) { cellValue = (short)newValue; } else if (newValue is bool) { cellValue = ((bool)newValue) ? 1 : 0; } else if (newValue == null || newValue == DBNull.Value) { cellValue = owner.NullImageIndex; } // Let the user map the value to an index MapIndexEventArgs mapArgs = new MapIndexEventArgs(e.ColumnIndex, e.RowIndex, newValue, cellValue); owner.OnMapValueToIndex(mapArgs); cellValue = mapArgs.Index + 1; // Cycle back to zero? if (cellValue < 0 || cellValue >= owner.ImageList.Images.Count) { cellValue = 0; } // If set to the null image index, set it to null. Otherwise, use the value. if (cellValue != owner.NullImageIndex) { // Let the user map the index back to a value mapArgs.Index = cellValue; mapArgs.Value = cellValue; owner.OnMapIndexToValue(mapArgs); // If the value changed, use that. If the value didn't change, use the new index value but // convert it to the matching type. if (mapArgs.Value == null || !mapArgs.Value.Equals(cellValue)) { base.NewValue = mapArgs.Value; } else if (newValue is int) { base.NewValue = cellValue; } else if (newValue is short) { base.NewValue = (short)cellValue; } else if (newValue is bool) { if (cellValue == 1) { base.NewValue = true; } else { base.NewValue = false; } } else { base.NewValue = mapArgs.Value; } } else { base.NewValue = null; } base.DataGridView.NotifyCurrentCellDirty(true); base.DataGridView.InvalidateCell(this); } }
/// <summary> /// This is overridden to cycle through the image index values when the cell is clicked and the cell is /// editable. /// </summary> /// <param name="e">The event arguments</param> protected override void OnContentClick(DataGridViewCellEventArgs e) { ImageListColumn owner = base.OwningColumn as ImageListColumn; int cellValue = -1; object newValue = base.NewValue; if(!base.ReadOnly && base.DataGridView != null && owner != null && owner.ImageList != null) { if(newValue is int) cellValue = (int)newValue; else if(newValue is short) cellValue = (short)newValue; else if(newValue is bool) cellValue = ((bool)newValue) ? 1 : 0; else if(newValue == null || newValue == DBNull.Value) cellValue = owner.NullImageIndex; // Let the user map the value to an index MapIndexEventArgs mapArgs = new MapIndexEventArgs(e.ColumnIndex, e.RowIndex, newValue, cellValue); owner.OnMapValueToIndex(mapArgs); cellValue = mapArgs.Index + 1; // Cycle back to zero? if(cellValue < 0 || cellValue >= owner.ImageList.Images.Count) cellValue = 0; // If set to the null image index, set it to null. Otherwise, use the value. if(cellValue != owner.NullImageIndex) { // Let the user map the index back to a value mapArgs.Index = cellValue; mapArgs.Value = cellValue; owner.OnMapIndexToValue(mapArgs); // If the value changed, use that. If the value didn't change, use the new index value but // convert it to the matching type. if(mapArgs.Value == null || !mapArgs.Value.Equals(cellValue)) base.NewValue = mapArgs.Value; else if(newValue is int) base.NewValue = cellValue; else if(newValue is short) base.NewValue = (short)cellValue; else if(newValue is bool) { if(cellValue == 1) base.NewValue = true; else base.NewValue = false; } else base.NewValue = mapArgs.Value; } else base.NewValue = null; base.DataGridView.NotifyCurrentCellDirty(true); base.DataGridView.InvalidateCell(this); } }
/// <summary> /// Gets the image to display in the cell /// </summary> /// <param name="value">The value to be use in determining the image</param> /// <param name="rowIndex">The index of the cell's parent row</param> /// <returns>The image that should be displayed in the cell</returns> protected override object GetCellImage(object value, int rowIndex) { // Use the image from the image list ImageListColumn owner = base.OwningColumn as ImageListColumn; Image cellImage = null; int cellValue = -1; if(owner != null && owner.ImageList != null) { if(value is int) cellValue = (int)value; else if(value is short) cellValue = (short)value; else if(value is bool) cellValue = ((bool)value) ? 1 : 0; // Let the user map the value to an index MapIndexEventArgs mapArgs = new MapIndexEventArgs(owner.Index, rowIndex, value, cellValue); owner.OnMapValueToIndex(mapArgs); cellValue = mapArgs.Index; if(cellValue < 0 || cellValue >= owner.ImageList.Images.Count) { cellValue = owner.NullImageIndex; if(cellValue >= owner.ImageList.Images.Count) cellValue = -1; } if(cellValue != -1) cellImage = owner.ImageList.Images[cellValue]; else cellImage = owner.NullImage; } return cellImage; }
/// <summary> /// This raises the <see cref="MapValueToIndex"/> event /// </summary> /// <param name="e">The event arguments</param> protected internal virtual void OnMapValueToIndex(MapIndexEventArgs e) { var handler = MapValueToIndex; if(handler != null) handler(this, e); }