您的位置:首页 > 编程语言 > C#

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

2013-03-26 10:56 399 查看
小弟最近看到某一元件的samplecode,利用DataGridView結合了DateTimePicker

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

c#(winfrom)

MainForm.cs(DateTimePicker範例)

01
using
System;
02
using
System.Collections.Generic;
03
using
System.ComponentModel;
04
using
System.Data;
05
using
System.Drawing;
06
using
System.Text;
07
using
System.Windows.Forms;
08
09
namespace

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,EventArgse)
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,DataGridViewCellEventArgse)
31
{
32
if

(
this
.dataGridView1.Columns[e.ColumnIndex].HeaderText==
"date"
)

33
{
34
Rectangler=
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,EventArgse)
51
{
52
if

(_CheckChange)
return
;
53
this
.dataGridView1.CurrentCell.Value=
this
.dateTimePicker1.Text;
54
}
55
}

56
}
執行結果:

DateTimePicker





NumericUpDown





RadioButton





http://www.dotblogs.com.tw/puma/archive/2008/11/10/5943.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: