我的自定义控件在页面中为何不能显示?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我的自定义控件在页面中为何不能显示?相关的知识,希望对你有一定的参考价值。
参考技术A 我分析有以下几个可能的原因:1.也许你把控件的可见属性设为了不可见,VISABLE;
2.有一些控件本身就为看不见的,比如时间控件;
3.也许你其他控件的属性设置与你看不见的那个属性相冲突,这样也有可能看不见.
C# 在 DataGridView 上显示我的自定义控件,即使不编辑
【中文标题】C# 在 DataGridView 上显示我的自定义控件,即使不编辑【英文标题】:C# show my custom control on DataGridView even if not editing 【发布时间】:2021-08-11 13:13:08 【问题描述】:我正在使用以下类来创建一个带有数字上下控件的自定义列。这工作得很好,但我想一直显示数字上下控制(即使不编辑)。以下代码将字符串值放在控件上,单击单元格后将显示数字控件进行编辑。我认为我需要重写 Paint 方法但我不知道如何实现它
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Utilities.General.Classes
public class NumericUpDownColumn : DataGridViewColumn
public NumericUpDownColumn() : base(new NumericUpDownCell())
public override DataGridViewCell CellTemplate
get return base.CellTemplate;
set
//Ensure that the cell used for the template is a NumericUpDown
if(value?.GetType().IsAssignableFrom(typeof(NumericUpDownCell)) == false)
throw new InvalidCastException("Must be a string");
base.CellTemplate = value;
public class NumericUpDownCell : DataGridViewTextBoxCell
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue,
DataGridViewCellStyle dataGridViewCellStyle)
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
if(DataGridView.EditingControl is NumericUpDown numeric)
numeric.Value = Convert.ToDecimal(Value ?? DefaultNewRowValue);
numeric.Minimum = MinValue;
numeric.Maximum = MaxValue;
protected override void OnEnter(int rowIndex, bool throughMouseClick)
if(throughMouseClick)
DataGridView.BeginEdit(false);
public override Type EditType
get return typeof(NumericUpDownEditingControl);
public override Type ValueType
get return typeof(string);
public override object DefaultNewRowValue
get return 0;
private int MinValue get; set;
private int MaxValue get; set;
public void UpdateValues(int minValue, int maxValue)
MinValue = minValue;
MaxValue = maxValue;
ReadOnly = MinValue == MaxValue;
if(ReadOnly)
Style.ForeColor = Color.DimGray;
public class NumericUpDownEditingControl : NumericUpDown, IDataGridViewEditingControl
public object EditingControlFormattedValue
get => Value;
set
if(value is decimal number)
try
Value = number;
catch
Value = 0;
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
if((context & DataGridViewDataErrorContexts.Parsing) != 0)
return Value.ToString();
return EditingControlFormattedValue;
public decimal MinValue
get => Minimum;
set => Minimum = value;
public decimal MaxValue
get => Maximum;
set => Maximum = value;
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
Font = dataGridViewCellStyle.Font;
ForeColor = dataGridViewCellStyle.ForeColor;
BackColor = dataGridViewCellStyle.BackColor;
public int EditingControlRowIndex get; set;
public bool EditingControlWantsInputKey(Keys key, bool datagridviewWantsInputKey)
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;
public void PrepareEditingControlForEdit(bool selectAll)
public bool RepositionEditingControlOnValueChange => false;
public DataGridView EditingControlDataGridView get; set;
public bool EditingControlValueChanged get; set;
public Cursor EditingPanelCursor => base.Cursor;
protected override void OnValueChanged(EventArgs eventargs)
EditingControlValueChanged = true;
EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
【问题讨论】:
【参考方案1】:这似乎有效:
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
if(decimal.TryParse(value.ToString(), out var val))
var ctrl = new NumericUpDown()
Value = val
;
var img = new Bitmap(cellBounds.Width, cellBounds.Height);
ctrl.DrawToBitmap(img, new Rectangle(0, 0, ctrl.Width, ctrl.Height));
graphics.DrawImage(img, cellBounds.Location);
else
throw new InvalidCastException($"value is not decimal type");
【讨论】:
以上是关于我的自定义控件在页面中为何不能显示?的主要内容,如果未能解决你的问题,请参考以下文章
C# 在 DataGridView 上显示我的自定义控件,即使不编辑