[c#]Winform DataGridView 結合 DateTimePicker、NumericUpDown與RadioButton的應用

小弟最近看到某一元件的sample code,利用DataGridView結合了DateTimePicker

感覺還不錯用,另外我用此方法在增加了NumericUpDown與RadioButton的整合,分享給大家呀..

c#(winfrom)
MainForm.cs(DateTimePicker範例)

 

01using System;
02using System.Collections.Generic;
03using System.ComponentModel;
04using System.Data;
05using System.Drawing;
06using System.Text;
07using System.Windows.Forms;
08  
09namespace DataGridViewDateTimePicker
10{
11    public partial class MainForm : Form
12    {
13        private bool _CheckChange = false;
14  
15        public MainForm()
16        {
17            InitializeComponent();
18        }
19  
20        private void MainForm_Load(object sender, EventArgs e)
21        {
22            // TODO: 這行程式碼會將資料載入 'database1DataSet.Table1' 資料表。您可以視需要進行移動或移除。
23            this.table1TableAdapter.Fill(this.database1DataSet.Table1);
24  
25            //設定DateTimePicker的高度
26            this.dateTimePicker1.Height = this.dataGridView1.Height;
27        }
28  
29        //將DateTimePicker控制項定位在DataGridView的Column上
30        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
31        {
32            if (this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "date")
33            {
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;
42            }
43            else
44            {
45                this.dateTimePicker1.Visible = false;
46            }
47        }
48  
49        //改變Column的值
50        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
51        {
52            if (_CheckChange) return;
53            this.dataGridView1.CurrentCell.Value = this.dateTimePicker1.Text;
54        }
55    }
56}

 


執行結果:

DateTimePicker

 

NumericUpDown

 

RadioButton

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值