由於時間原因,再加之自己懶惰!好久沒寫博客了,今天寫一篇關於datagridview中如何實現時間控件的文章,算是繼續自己的記錄的習慣!
首先,我們知道datagridview中並不提供控件,因此,我們無法直接使用!對此我們可以進行如下操作,從而實現在datagridview中插入時間控件。
一、聲明控件
我們首先要做的便是聲明一個時間控件,這個不必詳細多說了,有兩種方式:
1、直接在工具欄中進行拖拽
2、在窗體的designer.cs文件中進行聲明。
無論哪種方式,都可以,代碼如下:
private DateTimePicker date;
//
// date
//
this.date.Location = new System.Drawing.Point(230, 41);
this.date.Name = "date";
this.date.Size = new System.Drawing.Size(200, 25);
this.date.TabIndex = 18;
this.date.Visible = false; //這裏是讓控件先進行隱藏
this.date.ValueChanged += new System.EventHandler(this.date_ValueChanged);//該事件是對時間控件值改變時的操作,具體實現內容後邊將進行說明。
這樣,我們就將時間控件聲明好了!
二、進行賦值
//全局變量
private DataGridViewTextBoxCell partytime;
private void gridX_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
#region 如何顯示入黨時間控件
//date.left:表示控件在工作區距離左邊的距離(像素)。該工作區指的是窗體。
//this.gridX.left:gridX是datagridview名字,這裏是獲得datagridview控件與工作區左邊緣的距離,
//this.gridX.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).X:返回選擇單元格的左邊座標,該座標值是相對與datagridview左邊距離
//這樣我們就可以將時間控件的位置“畫在”工作區中,你可以理解爲我們拖着這個時間控件到了精確的位置。
date.Left = this.gridX.Left + this.gridX.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).X;
//該代碼同上,只是獲取的是上邊的距離。這樣實際我們就控制住了這個時間控件的左上角,至於大小我們可以自行設計。
date.Top = this.gridX.Top + this.gridX.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Y;
partytime = ((DataGridViewTextBoxCell)gridX.Rows[e.RowIndex].Cells["partyDate"]);//這裏聲明一個DataGridViewTextBoxCell用來獲取datagridview中partydate那列現在的值。
if (e.ColumnIndex == 9)//獲取所在列,注意datagridview中序號是從0開始的。
{
//下面代碼是讓控件顯示時間:1、若沒有時間,則控件時間顯示當前時間 2、若datagridview中有時間則顯示具體時間。
if (this.gridX.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null || this.gridX.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "")
{
this.date.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
this.date.Visible = true;
}
else
{
this.date.Value = Convert.ToDateTime(this.gridX.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
this.date.Visible = true;
}
}
else
{
this.date.Visible = false;
}
#endregion
}
三、控件日期選擇改變操作
private void date_ValueChanged(object sender, EventArgs e)
{
////this.dateTimePicker1.Visible = false;
partytime.Value = this.date.Value;//時間控件值改變在將其複製給聲明的那個DataGridViewTextBoxCell,然後進而改變datagridview相應列的值
this.date.Format = DateTimePickerFormat.Custom;
this.date.CustomFormat = "yyyy-MM-dd";
//this.gridX.CurrentCell.Value = this.date.Value;
//this.date.Visible = false;
}
至此時間控件在datagridview中的插入完畢!
同理,其實你可以嘗試其它控件到datagridview中,這裏我在將numericupanddown控件加在datagirdview中的代碼貼上:
一、聲明控件
private System.Windows.Forms.NumericUpDown beginYear;//聲明全局變量
//
// beginYear
//
this.beginYear.Location = new System.Drawing.Point(0, 0);
this.beginYear.Maximum = new decimal(new int[] {
3000,
0,
0,
0});
this.beginYear.Minimum = new decimal(new int[] {
2008,
0,
0,
0});
this.beginYear.Name = "beginYear";
this.beginYear.Size = new System.Drawing.Size(120, 25);
this.beginYear.TabIndex = 0;
this.beginYear.Value = new decimal(new int[] {
2015,
0,
0,
0});
this.beginYear.Visible = false;
this.beginYear.ValueChanged += new System.EventHandler(this.beginYear_ValueChanged);
二、實現控件的位置精確定位
private DataGridViewTextBoxCell beginyear;
beginYear.Left = gridX.Left + this.gridX.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).X;
beginYear.Top = gridX.Top + this.gridX.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Y;
beginyear = (DataGridViewTextBoxCell)(this.gridX.Rows[e.RowIndex].Cells["beginYear"]);
if (e.ColumnIndex == 12)
{
if (this.gridX.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null || this.gridX.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == DBNull.Value)
{
beginYear.Value = System.DateTime.Now.Year;
this.beginYear.Visible = true;
}
else
{
//beginYear.Value = Convert.ToDecimal(dataRow["beginYear"]);
beginYear.Value = Convert.ToDecimal(this.gridX.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
this.beginYear.Visible = true;
}
}
else
{
this.beginYear.Visible = false;
}
這裏有一點需要說明,要先精確定位在進行判斷,之所以這麼做是因爲當有兩個以上的該控件時會出現錯誤。具體原因我也不是很清楚!在這裏也希望大家能給出原因,在此謝謝了!
三、將值賦值到datagridview中
private void beginYear_ValueChanged(object sender, EventArgs e)
{
beginyear.Value = this.beginYear.Value;
// this.beginYear.Visible = false;
}
this.beginYear.Visible=false;之所以刪除是因爲,當你點擊一下向上或向下按鈕時,此控件就消失了,這種效果並不是我們所要的。包括DateTimePicker控件,如果設置了此屬性在這裏,將會出現每當你點擊選中無論是年份、月份、還是日都會消失,而去掉則會只有當你點擊到具體的日時纔會消失。這個效果當時是微軟在定義DateTimePicker控件是設定好的,我們只是藉助此效果。再次給大家送上晚年祝福!!
元宵節快樂!