C # select a date/drop-down list box by clicking a column in the DataGridView.

Source: Internet
Author: User

I encountered this problem when I was working on the project today. After searching for examples on the internet, I finally found a suitable one from csdn for a long time. Now I have published it, for your reference (just select the-_-of the date -_-)

1. First declare the DateTimePicker object (public)
Private DateTimePicker dTimePicker = new DateTimePicker ();


2. Add the following code in Form_Load:
DTimePicker. Visible = false; // not displayed by default
DTimePicker. Format = DateTimePickerFormat. Custom; // The DateTimePicker Format is Custom dTimePicker. CustomFormat = "yyyy-MM-dd HH: mm: ss"; // DateTimePicker Custom type
DTimePicker. TextChanged + = new EventHandler (dTimePicker_TextChanged); // value assignment when DateTimePicker text changes

 

Private void dTimePicker_TextChanged (object sender, EventArgs e)
{
// Assign the Text value of dTimePicker to the selected cell value of dataGridView1
DgvFKDB. CurrentCell. Value = dTimePicker. Text;
}


Private void dgvFKDB_CurrentCellChanged (object sender, EventArgs e)
{
DataGridViewCell CurrnetCell = this. dgvFKDB. CurrentCell;
If (CurrnetCell! = Null & CurrnetCell. OwningColumn. Name = "FKSJ ")
{
// Obtain the rectangle of the display area of the selected cell in dataGridView1
Rectangle Rect = this. dgvFKDB. GetCellDisplayRectangle (CurrnetCell. ColumnIndex, CurrnetCell. RowIndex, true );
// DTimePicker is displayed in the rectangle of the display area of the selected cell in dataGridView1, that is, within the selected cell
DTimePicker. Visible = true;
DTimePicker. Top = Rect. Top;
DTimePicker. Left = Rect. Left;
DTimePicker. Height = Rect. Height;
DTimePicker. Width = Rect. Width;
// The dTimePicker control obtains the value of the selected cell in the initial value of dataGridView1.
DTimePicker. Text = dgvFKDB. Rows [CurrnetCell. RowIndex]. Cells [CurrnetCell. ColumnIndex]. Value. ToString ();
}
Else
{
This. dTimePicker. Visible = false;
}
}

This example is adapted from csdn.

Download source code http://www.bkjia.com/uploadfile/2012/0321/20120321083518126.rar (csdn above, can refer)
 

From yilou xianyu
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.