轉載於 F6 Team "用心" by puma

Winform DataGridView 結合 DateTimePicker、NumericUpDown與RadioButton的應用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DataGridViewDateTimePicker
{
public partial class MainForm : Form
{
private bool _CheckChange = false;

public MainForm()
{
InitializeComponent();
}

private void MainForm_Load(object sender, EventArgs e)
{
// TODO: 這行程式碼會將資料載入 'database1DataSet.Table1' 資料表。您可以視需要進行移動或移除。
this.table1TableAdapter.Fill(this.database1DataSet.Table1);

//設定DateTimePicker的高度
this.dateTimePicker1.Height = this.dataGridView1.Height;
}

//將DateTimePicker控制項定位在DataGridView的Column上
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "date")
{
Rectangle r = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
r = this.dataGridView1.RectangleToScreen(r);
this.dateTimePicker1.Location = this.RectangleToClient(r).Location;
this.dateTimePicker1.Size = r.Size;
this._CheckChange = true;
this.dateTimePicker1.Text = this.dataGridView1.CurrentCell.Value.ToString();
this._CheckChange = false;
this.dateTimePicker1.Visible = true;
}
else
{
this.dateTimePicker1.Visible = false;
}
}

//改變Column的值
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
if (_CheckChange) return;
this.dataGridView1.CurrentCell.Value = this.dateTimePicker1.Text;
}
}
}

執行結果:

DateTimePicker

20081110231629488.jpg

NumericUpDown

20081110231658208.jpg

RadioButton

20081110231732497.jpg

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 kingjoy1235 的頭像
    kingjoy1235

    King的幸福國度

    kingjoy1235 發表在 痞客邦 留言(0) 人氣()