DataGridView中的UserControl

时间:2014-10-27 14:20:36

标签: c# datagridview custom-cell

我创建了一个包含文本框,按钮和月视图的用户控件。我按照说明编写了DGV列,单元格和编辑控件类: http://msdn.microsoft.com/en-us/library/vstudio/7tas5c80(v=vs.100).aspx

然后我添加了对我的Checkbook程序的引用,该程序使用数据网格视图作为检查寄存器。此特定列用于选择交易日期。当用户点击小按钮时,我希望月份日历下拉,因此可以选择日期。

问题是当我点击按钮时似乎没有任何反应。当我扩展列/单元格宽度&高度我可以看到日历并选择日期。

我需要知道如何在单击按钮后将DGV单元格更改为整个用户控件的大小,然后在选择日期后恢复正常。我希望这只发生在单元格中,而不是像我必须手动操作那样使行高变化。

我需要掌握这一点,因为其他单元格需要收款人和类别以及计算器的列表框。

public class DGVTxtBtnEditControl : txtBtn, IDataGridViewEditingControl
{
    // The grid that owns this editing control
    private DataGridView dataGridView;
    // Stores whether the editing control's value has changed or not
    private bool valueChanged;
    // Stores the row index in which the editing control resides
    private int rowIndex;


    public DGVTxtBtnEditControl()
    {
        this.TabStop = false;
    }

    public DataGridView EditingControlDataGridView
    {
        get
        {
            return dataGridView;
        }
        set
        {
            dataGridView = value;
        }
    }

    public object EditingControlFormattedValue
    {
        get
        {
            return this.Date;
        }
        set
        {
            this.EditingControlFormattedValue = DateTime.Parse((String)value);
        }
    }

    public int EditingControlRowIndex
    {
        get { return rowIndex; }
        set { rowIndex = value; }
    }

    public bool EditingControlValueChanged
    {
        get { return this.valueChanged; }
        set { this.valueChanged = value; }
    }

    public bool RepositionEditingControlOnValueChange
    {
        get { return false; }
    }

    public Cursor EditingPanelCursor
    {
        get { return Cursors.Default; }
    }

    public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
    {
        switch (keyData & Keys.KeyCode)
        {
            case Keys.Right:
            case Keys.Left:
            case Keys.Tab:
            case Keys.Enter:
                dataGridViewWantsInputKey = true;
                return false;
            default:
                dataGridViewWantsInputKey = false;
                return true;

        }
    }

    public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
    {
        return EditingControlFormattedValue;
    }

    public void PrepareEditingControlForEdit(bool selectAll)
    {
        // No preparation needs to be done.
    }

    public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
    {
        this.Font = dataGridViewCellStyle.Font;
        this.BackColor = dataGridViewCellStyle.BackColor;
        this.ForeColor = dataGridViewCellStyle.ForeColor;
    }

    protected override void OnTextChanged(EventArgs e)
    {
        // Notify the DataGridView that the contents of the cell
        // have changed.
        valueChanged = true;
        this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
        base.OnTextChanged(e);
    }

    private void NotifyDataGridViewOfValueChange()
    {
        if (!this.valueChanged)
        {
            this.valueChanged = true;
            this.dataGridView.NotifyCurrentCellDirty(true);
        }
    }
}

public class CalendarCell : DataGridViewTextBoxCell
{
    #region Default constants for CalendarCell
    // Default values for DataGridViewCustomEditCell  
    internal const int CalendarCell_defaultMaxLength = 20;
    // Default dimensions of the static rendering bitmap used for the painting   
    // of the non-edited cells  
    private const int CalendarCell_defaultRenderingBitmapWidth = 100;
    private const int CalendarCell_defaultRenderingBitmapHeight = 22;
    #endregion

    #region Private Properties
    // Type of this cell's editing control
    private static Type defaultEditType = typeof(DGVTxtBtnEditControl);

    // The bitmap used to paint the non-edited cells via a call to TextBox.DrawToBitmap
    [ThreadStatic]
    private static Bitmap renderingBitmap;

    // The TextBox control used to paint the non-edited cells via a call to TextBox.DrawToBitmap
    [ThreadStatic]
    private static TextBox paintingTextbox;
    #endregion

    #region Constructor
    public CalendarCell()
        : base()
    {
        // Use the short date format.
        this.Style.Format = "d";
        // Create a thread specific bitmap used for the painting of the non-edited cells
        if (renderingBitmap == null)
        {
            renderingBitmap = new Bitmap(CalendarCell_defaultRenderingBitmapWidth, CalendarCell_defaultRenderingBitmapHeight);
        }

        // Create a thread specific EllipsesTextbox control used for the painting of the non-edited cells
        if (paintingTextbox == null)
        {
             paintingTextbox = new TextBox();
            // Some properties only need to be set once for the lifetime of the control:
            paintingTextbox.BorderStyle = BorderStyle.None;
        }
        // Set the Default Properties
        this.MaxInputLength = CalendarCell_defaultMaxLength;
    }
    #endregion

    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);
        DGVTxtBtnEditControl ctl =
            DataGridView.EditingControl as DGVTxtBtnEditControl;
        // Use the default row value when Value property is null.
        if (this.Value == null)
        {
            ctl.Date = Convert.ToString(this.DefaultNewRowValue);
        }
        else
        {
            ctl.Date = Convert.ToString(this.Value);
        }

        //DGVTxtBtnEditControl.Size = new System.Drawing.Size(328, 190);
    }

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

        }
    }

    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;
        }
    }
}

public class CalendarColumn : DataGridViewColumn
{
    public CalendarColumn()
        : 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 partial class txtBtn : UserControl
{       
    private string Odate ;
    private string Sdate;

    public string Date
    {
        get 
        {
            Odate = this.textBox1.Text;
            return Odate; 
        }
        set
        { 
            Odate = value; 
        }
    }

    public string SelDate
    {
        get
        {
            Sdate = Convert.ToString(this.monthCalendar1.SelectionRange);
            return Sdate;
        }
        set
        {
            Sdate = value;
        }
    }

    public txtBtn()
    {            
        InitializeComponent();
        this.btn1.Visible = false;
        this.monthCalendar1.Visible = false;
        this.textBox1.Text = DateTime.Now.ToString("MM-dd-yyyy");
        string Value = this.textBox1.Text;
    }

    protected override void OnGotFocus(EventArgs e)
    {
        base.OnGotFocus(e);
        this.textBox1.Focus();
    }

    private void textBox1_Click(object sender, EventArgs e)
    {
        this.btn1.Visible = true;
    }

    public void btn1_MouseClick(object sender, MouseEventArgs e)
    {
        monthCalendar1.Location = new Point(e.X + btn1.Location.X, e.Y + btn1.Location.Y);
        monthCalendar1.Visible = true;
    }

    public void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
    {
        this.textBox1.Text = monthCalendar1.SelectionStart.ToShortDateString();
        this.monthCalendar1.Visible = false;
    }

    public void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
    {
        e.Start.Date.ToShortDateString();
    }

    private void txtMemo_TextChanged(object sender, EventArgs e)
    {
        this.OnTextChanged(e);
    }

    public void btn1_Click_1(object sender, EventArgs e)
    {
        monthCalendar1.Show();                       
    }    

}

0 个答案:

没有答案