C# 在 DataGridView 上显示我的自定义控件,即使不编辑

Posted

技术标签:

【中文标题】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 上显示我的自定义控件,即使不编辑的主要内容,如果未能解决你的问题,请参考以下文章

在 c# 和 ms 访问中使用 DataGridView 超链接有困难

webform c#中的datagridview在页脚中显示查询

c#如何实现DataGridView翻页显示

C#中能不能用datagridview显示文件的内容?不用数据库

c# datagridview 绑定数据时逐行显示并且有进度条。问题如下:

C#编写的一个界面 怎样实现dataGridView1里的内容在上面显示?