如何在DataGridView中手动生成的列的所有单元格中显示DateTimePicker?

时间:2016-12-12 07:45:31

标签: c# winforms

我的Windows窗体应用程序中有一个dataGridview,我正在尝试在手动生成的列的所有单元格中生成日期选择器。我的下面的代码如下。

private void Form1_Load(object sender, EventArgs e)
    {
        using (SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))
        {
            string query1 = "SELECT [Full] FROM database table";

            sqlCon.Open();

            using (SqlCommand command1 = new SqlCommand(query1, sqlCon))
            {
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(command1);
                da.Fill(dt);
                dataGridView2.AutoGenerateColumns = false;
                dataGridView2.ColumnCount = 3;
                dataGridView2.Columns[0].HeaderText = "Test";
                dataGridView2.Columns[0].DataPropertyName = "Full";
                dataGridView2.Columns[1].HeaderText = "Date";

              //timedate picker here
              //dataGridView2.Columns[1].
                dataGridView2.Columns[2].HeaderText = "Result";
                dataGridView2.DataSource = dt;
                da.Dispose();
            }
            sqlCon.Close();
        }       
    }

2 个答案:

答案 0 :(得分:0)

它不是开箱即用的工具,但是将System.Windows.Forms.DataGridViewColumn子类化为托管您选择的任何控制并不是那么困难。最重要的是,System.Windows.Forms命名空间已包含DateTimePicker类,但此时它不适合在DataGridView内使用。让我们将这两者粘合在一起。

我们首先要创建一个DataGridViewCalendarColumn类:

public class DataGridViewCalendarColumn : DataGridViewColumn
{
    public DataGridViewCalendarColumn() : base(new DataGridViewCalendarCell())
    {

    }

    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(DataGridViewCalendarCell)))
            {
                throw new InvalidCastException("Must be a DataGridViewCalendarCell");
            }
            base.CellTemplate = value;
        }
    }
}

那么这门课做什么?它基本上说好了,DataGridViewColumn期望其单元格CellTemplate。这是一个约束,可确保我们添加到DataGridView的任何列都符合某个标准。

从本质上讲,我们可以欺骗DataGridView来接受包含新类型单元格的新类型。但此时DataGridViewCalendarCell还没有存在,所以我们也会继续创建它:

我们首先要对DataGridViewTextBoxCell进行子类化,因为我们必须从DataGridView确实识别的一种单元格开始。在这种情况下DataGridViewTextBoxCell

public class DataGridViewCalendarCell : DataGridViewTextBoxCell
{
    public DataGridViewCalendarCell()
    : 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);
        DataGridViewCalendarEditingControl ctl =
            DataGridView.EditingControl as DataGridViewCalendarEditingControl;
        // Use the default row value when Value property is null.
        if (this.Value != null || !String.IsNullOrEmpty(initialFormattedValue.ToString()))
        {
            DateTime parsedDate;
            bool IsDate = DateTime.TryParseExact(this.Value.ToString(), "d/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate);
            if (IsDate)
            {
                ctl.Value = parsedDate;
            }
        }
    }

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

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

            return typeof(DateTime);
        }
    }

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

所以这基本上是对我们的DataGridView说的:看,我知道,你知道你已经习惯接受DataGridViewTextBoxCell,因为这是我们创造的方式可以显示和编辑文本的网格。所以我要向你扔一个的文本单元格。

此外,我们会将输入值限制为可解析为DateTime的文本。

这会创建一个参考框架,这样如果我们在单击单元格时尝试创建实际的日历控件,我们的网格就不会感到惊讶。所以现在真正的魔力:实际的日历控制!

我们将对常规DateTimePicker控件进行子类化,但也使其实现IDataGridViewEditingControl接口,该接口将任何希望编辑DataGridView内容的控件绑定到a具体的规则。

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

    public DataGridViewCalendarEditingControl()
    {
        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 if the string is 
                    // null, empty, or not in the format of a date.
                    DateTime parsedDate;
                    bool IsDate = DateTime.TryParseExact((String)value, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate);
                    this.Value = (IsDate) ? parsedDate : DateTime.Now;
                }
                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);
    }
}

这是代码的核心。我意识到这似乎很多,但是一旦掌握了这一点,你就可以创建你需要的各种DataGridView列,单元格和控件。

您从数据库中提取的数据(即数据库端的DATETIME列)可以保留为字符串格式。我们刚刚创建的控件将这些字符串解析为日期。请填写DataTable,以编程方式添加列并绑定。假设您为DataTable填充了两列,其中一列包含日期:

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("dbTextColName", typeof(string)));
dt.Columns.Add(new DataColumn("dbDateTimeColName", typeof(string))); // yes string, our code above will take care of the parsing

//从db填充你的DataTable的逻辑到这里

var colText = new DataGridViewTextBoxColumn();
colText.DataPropertyName = "dbTextColName";
colText.HeaderText = "MyTextColumn";
colText.Name = "dbTextColName";

var colDateTime = new DataGridViewCalendarColumn();
colDateTime.DataPropertyName = "dbDateTimeColName";
colDateTime.HeaderText = "MyDateTimeColumn";
colDateTime.Name = "dbDateTimeColName";

yourDataGridView.Columns.AddRange(new DataGridViewColumn[] {colText, colDateTime});

yourDataGridView.DataSource = dt;

答案 1 :(得分:0)

捕获网格的onclick事件,如果是您创建的列,则在日历组件上将visible设置为true,或者在xy坐标列的网格顶部设置DateTimePicker组件。无论他们选择哪个日期,都要将该日期的字符串值放入他们点击的单元格中,并隐藏日历组件。