本文介绍了将datetimepicker放在datagridview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在datagridview中编辑单元格时,我想将datetimepicker放在该单元格中。
我写了这段代码,但这似乎不起作用。
任何人都可以帮我吗?
When a cell is edited in datagridview, I want to place datetimepicker in that cell.
I have written this piece of code but this doesn't seem to work.
Can any one help me in this?
void DataGridView1DoubleClick(object sender, EventArgs e)
{
MouseEventArgs args = (MouseEventArgs)e;
Point p = dataGridView1.PointToClient(new Point(args.X,args.Y));
DataGridView.HitTestInfo info = dataGridView1.HitTest(p.X, p.Y);
int Col=dataGridView1.CurrentCellAddress.X;
if(dataGridView1.Columns[Col].Name == "Employee name";)
{
// textBox1.text="hello";
// textBox1.SetBounds(args.X,args.Y,dataGridView1.CurrentCell.Size.Width,dataGridView1.CurrentCell.Size.Height);
// textBox1.Show();
// DataGridTextBox.TextBox.Controls.Add( textBox1 );
// textBox1.Text = DataGridView[DataGridView1.CurrentRowIndex , Col].ToString();
// textBox1.Focus();
}
if(Col==3)
{
//placing date time picker here..
}
}
提前付款。
thanks in advance.
推荐答案
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int myRow = 2; // we'll place this functionality
int myCol = 2; // in the 2nd row and column of the control
if ((e.RowIndex == myRow) & (e.ColumnIndex == myCol))
{
DateTimeForm myDTForm = new DateTimeForm();
if (myDTForm.ShowDialog()== DialogResult.OK)
{
// put the dialog result into the datagridview cell
((DataGridView)sender)[myCol, myRow].Value = myDTForm.Tag;
((DataGridView)sender).Invalidate(); // to update the grid
}
}
}
我没有测试这段代码。我在搜索其他查询时找到了它。
如果那不起作用,请告诉我,以便我可以尝试修复代码。
I did not test this code. I found it while I was searching for some other query.
If that is not working let me know so that I can try fixing the code.
private DateTimePicker cellDateTimePicker;
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
this.cellDateTimePicker = new DateTimePicker();
this.cellDateTimePicker.ValueChanged += new EventHandler(cellDateTimePickerValueChanged);
this.cellDateTimePicker.Visible = false;
this.dataGridView1.Controls.Add(cellDateTimePicker);
if (e.ColumnIndex == column index which u want)
{
Rectangle tempRect = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
cellDateTimePicker.Location = tempRect.Location;
cellDateTimePicker.Width = tempRect.Width;
cellDateTimePicker.Visible = true;
}
void cellDateTimePickerValueChanged(object sender, EventArgs e)
{
dataGridView1.CurrentCell.Value = cellDateTimePicker.Value.ToString();//convert the date as per your format
cellDateTimePicker.Visible = false;
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == column index which u want)
{
Rectangle tempRect = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
cellDateTimePicker.Location = tempRect.Location;
cellDateTimePicker.Width = tempRect.Width;
cellDateTimePicker.Visible = true;
}
}
这篇关于将datetimepicker放在datagridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!