小弟最近看到某一元件的sample code,利用DataGridView結合了DateTimePicker
感覺還不錯用,另外我用此方法在增加了NumericUpDown與RadioButton的整合,分享給大家呀..
c#(winfrom)
MainForm.cs(DateTimePicker範例)
02 | using System.Collections.Generic; |
03 | using System.ComponentModel; |
07 | using System.Windows.Forms; |
09 | namespace DataGridViewDateTimePicker |
11 | public partial class MainForm : Form |
13 | private bool _CheckChange = false ; |
17 | InitializeComponent(); |
20 | private void MainForm_Load( object sender, EventArgs e) |
23 | this .table1TableAdapter.Fill( this .database1DataSet.Table1); |
26 | this .dateTimePicker1.Height = this .dataGridView1.Height; |
30 | private void dataGridView1_CellEnter( object sender, DataGridViewCellEventArgs e) |
32 | if ( this .dataGridView1.Columns[e.ColumnIndex].HeaderText == "date" ) |
34 | Rectangle r = this .dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false ); |
35 | r = this .dataGridView1.RectangleToScreen(r); |
36 | this .dateTimePicker1.Location = this .RectangleToClient(r).Location; |
37 | this .dateTimePicker1.Size = r.Size; |
38 | this ._CheckChange = true ; |
39 | this .dateTimePicker1.Text = this .dataGridView1.CurrentCell.Value.ToString(); |
40 | this ._CheckChange = false ; |
41 | this .dateTimePicker1.Visible = true ; |
45 | this .dateTimePicker1.Visible = false ; |
50 | private void dateTimePicker1_ValueChanged( object sender, EventArgs e) |
52 | if (_CheckChange) return ; |
53 | this .dataGridView1.CurrentCell.Value = this .dateTimePicker1.Text; |
執行結果:
DateTimePicker
NumericUpDown
RadioButton