DataGridView column DateTimePicker in vb.net C#

本文关键字:net vb in column DateTimePicker DataGridView | 更新日期: 2023-09-27 18:36:28

我正在使用 DataGridView 内部 DateTimePicker,但它无法正常工作。它只能选择年或月,日历控件已关闭。如何选择年份、月份和日期?

DataGridView column DateTimePicker in vb.net C#

试试这段代码(c# 代码)

DateTimePicker dtp = new DateTimePicker();  //DateTimePicker
Rectangle _Rectangle;
public Form2()
    {
        InitializeComponent();

        dataGridView2.Controls.Add(dtp);
        dtp.Visible = false;  //  
        dtp.Format = DateTimePickerFormat.Custom;  //2010-08-05  
        dtp.TextChanged += new EventHandler(dtp_TextChange);

    }
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        switch (dataGridView2.Columns[e.ColumnIndex].Name)
        {
            case "dateAchatDataGridViewTextBoxColumn1":
                _Rectangle = dataGridView2.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true); //  
                dtp.Size = new Size(_Rectangle.Width, _Rectangle.Height); //  
                dtp.Location = new Point(_Rectangle.X, _Rectangle.Y); //  
                dtp.Visible = true;  
                break;
        }
    }

    private void dtp_TextChange(object sender, EventArgs e)
    {
        dataGridView2.CurrentCell.Value = dtp.Text.ToString();  
    }  
    private void dataGridView2_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)  
    {  
        dtp.Visible = false;  
    }  

    private void dataGridView2_Scroll(object sender, ScrollEventArgs e)  
    {  
        dtp.Visible = false;  
    }

这是答案:
公共分部类 表格 1 : 表格 {

    private DateTimePicker cellDateTimePicker = new DateTimePicker();
    DateTimePicker[] sp = new DateTimePicker[100];
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        DataTable dt=new DataTable();
        dt.Columns.Add(new DataColumn("Start-Date", typeof(DateTime)));
        dataGridView1.DataSource = dt;
    }
    private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {

        sp[e.RowIndex] = new DateTimePicker();
        if (dataGridView1.Columns[e.ColumnIndex].Name == "Start-Date")
        {
            this.cellDateTimePicker = sp[e.RowIndex];
            this.cellDateTimePicker.Format = DateTimePickerFormat.Time;
                this.cellDateTimePicker.Name = "sp[" + e.RowIndex + "]";
         //   this.cellDateTimePicker.ValueChanged += new EventHandler(cellDateTimePickerValueChanged);
            this.cellDateTimePicker.Visible = true;
            this.cellDateTimePicker.CustomFormat = "dd/MM/yyyy";
            this.cellDateTimePicker.Format = DateTimePickerFormat.Custom;
             this.dataGridView1.Controls.Add(cellDateTimePicker);
            System.Drawing.Rectangle tempRect = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
            cellDateTimePicker.Location = tempRect.Location;
            cellDateTimePicker.Width = tempRect.Width;
 dataGridView1.Columns[0].DefaultCellStyle.Format = "MM'/'dd'/'yyyy";
          this.cellDateTimePicker.Visible = true;
        }
    }
}