DataGridView自定义列在运行时访问设计时间参数 [英] DataGridView custom column accessing design time parameters at run time

查看:132
本文介绍了DataGridView自定义列在运行时访问设计时间参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩一些示例代码,它为DataGridView实现了一个datepicker列。

I have been playing with some sample code which implements a datepicker column for a DataGridView.

我正在努力的是在如何适当地访问设计时间参数(感谢Bradley Smith的覆盖Clone方法的提示)在我想要的CalendarEditingControl中将格式设置为DateFormat。

Where I'm struggling is in how to appropriately access design time parameters (thanks to Bradley Smith for the tip on overriding the Clone method) in the CalendarEditingControl where I want to set the Format to the DateFormat.

下面提供了完整的示例,并查看我要使用DateFormat如果您只是扫描注释:
// * ** * ** * ** * * 'd喜欢将格式设置为在DataGridViewCalendarColumn.DateFormat中提供的设计时间值这里 * ** * ** * ** * //

I've provided the complete sample below and to see where I want to use DateFormat if you just scan for the comment: //*********** I'd like to set the Format to the design time value provided inDataGridViewCalendarColumn.DateFormat here ***********//

谢谢

Matt

使用系统;
使用System.Windows.Forms;

using System; using System.Windows.Forms;

命名空间MyApplication.Components
{
public class DataGridViewCalendarColumn:DataGridViewColumn
{
public DataGridViewCalendarColumn()
:base(new CalendarCell())
{
}

namespace MyApplication.Components { public class DataGridViewCalendarColumn : DataGridViewColumn { public DataGridViewCalendarColumn() : base(new CalendarCell()) { }

    public override DataGridViewCell CellTemplate
    {
        get
        {
            return base.CellTemplate;
        }
        set
        {
            // Ensure that the cell used for the template is a CalendarCell. 
            if (value != null &&
                !value.GetType().IsAssignableFrom(typeof(CalendarCell)))
            {
                throw new InvalidCastException("Must be a CalendarCell");
            }
            base.CellTemplate = value;

        }
    }

    public DateTimePickerFormat DateFormat { get; set; }

    public override object Clone()
    {
        DataGridViewCalendarColumn clone = (DataGridViewCalendarColumn)base.Clone();
        clone.DateFormat = this.DateFormat;
        return clone;
    }

}

public class CalendarCell : DataGridViewTextBoxCell
{

    public CalendarCell(): base()
    {
        // Use the short date format. 
        this.Style.Format = "d";
    }

    public override void 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;
        }
    }

    public override Type EditType
    {
        get
        {
            // Return the type of the editing control that CalendarCell uses. 
            return typeof(CalendarEditingControl);
        }
    }

    public override Type ValueType
    {
        get
        {
            // Return the type of the value that CalendarCell contains. 

            return typeof(DateTime);
        }
    }

    public override object DefaultNewRowValue
    {
        get
        {
            // Use the current date and time as the default value. 
            return DateTime.Now;
        }
    }


}

class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
{
    DataGridView dataGridView;
    private bool valueChanged = false;
    int rowIndex;

    public CalendarEditingControl()
    {
        //*********************** I'd like to set the Format to the design time value provided inDataGridViewCalendarColumn.DateFormat here ***********************//
        //(instead of hard coding it to DateTimePickerFormat.Short)
        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);
    }
}

}

推荐答案

所有您需要做的是将 DateFormat 属性从列中应用到编辑控件。在 InitializeEditingControl 方法中,添加以下行:

All you need to do is apply the DateFormat property from the column onto the editing control. In the InitializeEditingControl method, add the following line:

ctl.Format = (OwningColumn as DataGridViewCalendarColumn).DateFormat;

应该做的伎俩。

这篇关于DataGridView自定义列在运行时访问设计时间参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆