簡體   English   中英

datagridview中的日歷列c#

[英]calendar column c# in datagridview

基本上我想在我的Datagridview中編輯一些列時顯示日歷。

如何:Windows窗體中的主機控件來自MSDN的DataGridView單元格我可以在建築物中添加想要的類型的列。

但就我而言,我必須使用Excel閱讀器提供的數據源 ,它必須將標題連接到日期類型列。

private DataGridView AddCalendars(DataGridView dtgv)
{
  dtgv.DataSource = controller.getEmptyDataTable();     // DataTable provided by excel reader
  var l = dtgv.Columns.Count;
  string[] dateColumns = {"date_received", "date_of_birth"};
  for (var i = 0; i < l; ++i)
  {
    if ( dateColumns.Any( dtgv.Columns[i].HeaderText.Contains )
    {
      dtgv.Columns[i] = new CalendarColumn();      // this line does not work cause by readonly
    }
  }
  return dtgv;
}

如何將Calendar Column控件應用於所選列?

或者,如何通過構建Datagridview以不同的方式獲得相同的結果?

找到一個可以解決這個問題的解決方案http://www.codeproject.com/Questions/175124/placing-datetimepicker-in-datagridview

private DateTimePicker cellDateTimePicker;
private List<int> dateColumnsIndexes;

public MainForm()
{
  InitializeComponent();

  /// 
  this.cellDateTimePicker = new DateTimePicker();
  this.cellDateTimePicker.ValueChanged += new EventHandler(cellDateTimePickerValueChanged);
  this.cellDateTimePicker.Visible = false;
  this.cellDateTimePicker.CustomFormat = "dd/MM/yyyy";
  this.cellDateTimePicker.Format = DateTimePickerFormat.Custom;
  this.dataGridView1.Controls.Add(cellDateTimePicker);
  (...)
}

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
  var index = masterDataGridView.CurrentCell.ColumnIndex;
  if (this.dateColumnsIndexes.Contains(index))
  {
    Rectangle tempRect = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);          
    cellDateTimePicker.Location = tempRect.Location;
    cellDateTimePicker.Width = tempRect.Width;
    try
    {
      cellDateTimePicker.Value = DateTime.Parse(dataGridView1.CurrentCell.Value.ToString());
    }
    catch
    {
      cellDateTimePicker.Value = DateTime.Now;
    }
    cellDateTimePicker.Visible = true;
  }
}

void cellDateTimePickerValueChanged(object sender, EventArgs e)
{
  masterDataGridView.CurrentCell.Value = cellDateTimePicker.Value.ToString("dd/MM/yyyy");
  cellDateTimePicker.Visible = false;
}

private void AddCalendars(DataGridView dtgv)
{
  dateColumnsIndexes = new List<int>();
  dtgv.DataSource = controller.getEmptyDataTable();     // DataTable provided by excel reader
  var l = dtgv.Columns.Count;
  string[] dateColumns = {"date_received", "date_of_birth"};
  for (var i = 0; i < l; ++i)
  {
    if ( dateColumns.Any( dtgv.Columns[i].HeaderText.Contains )
    {
      dateColumnsIndexes.add(i);
    }
 }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM