C#DataGridView:GetCellDisplayRectangle。位置返回错误的值
在DataGridView中,当我在运行时调整列的长度,以修改其标题的高度(因为从一行切换到两行显示,或者反之亦然)时,GetCellDisplayRectangle.Location()仍然指示单元格的旧垂直位置。dataGridView1.Refresh()没有帮助。例如,如果我调整了表维度的大小(定位到表单),则会刷新位置信息。为什么呢?我能做什么?谢谢 更新: 实际上,我想要实现的是在DataGridView的第二列中有时间选择器。我知道这是一个老话题,我知道MSDN的例子,我不喜欢,因为它太大,太麻烦了,而且不完全是我需要的。我使用一个简单的解决方案:在每个新的DataGridView行中,我添加DateTimePicker,这些DateTimePicker配置为我想要的DataGridView控件,并将它们定位到所需的单元格位置。这个解决方案几乎奏效了:我的问题是,每次滚动或更改单元格的大小/高度时,我都必须重新定位/调整大小/隐藏/显示选择器。 附加DataGridView行的代码:C#DataGridView:GetCellDisplayRectangle。位置返回错误的值,c#,datagridview,C#,Datagridview,在DataGridView中,当我在运行时调整列的长度,以修改其标题的高度(因为从一行切换到两行显示,或者反之亦然)时,GetCellDisplayRectangle.Location()仍然指示单元格的旧垂直位置。dataGridView1.Refresh()没有帮助。例如,如果我调整了表维度的大小(定位到表单),则会刷新位置信息。为什么呢?我能做什么?谢谢 更新: 实际上,我想要实现的是在DataGridView的第二列中有时间选择器。我知道这是一个老话题,我知道MSDN的例子,我不喜欢,因
List<DateTimePicker> tplist;
int index;
int x, y;
private void button_append_Click( object sender, EventArgs e )
{
DateTimePicker tp;
tp = new DateTimePicker();
tp.Format = DateTimePickerFormat.Custom;
tp.CustomFormat = "mm:ss";
tp.ShowUpDown = true;
tp.Value = DateTime.Now.Date;
tp.Value = new DateTime( 2000, 1, 1, 0, index, 0 );
dataGridView1.Controls.Add( tp );
dataGridView1.Rows.Add( new object[] { "line " +
Convert.ToString( index ),
"val " + Convert.ToString( index ) } );
Rectangle frame = dataGridView1.GetCellDisplayRectangle( 1,
dataGridView1.RowCount - 1, false );
tp.Size = frame.Size;
tp.Location = frame.Location;
if( dataGridView1.RowCount <=
dataGridView1.FirstDisplayedCell.RowIndex +
dataGridView1.DisplayedRowCount( true ) )
tp.Visible = true;
else tp.Visible = false;
tplist.Add( tp );
if( tplist.Count == 1 )
{ x = tplist[ 0 ].Location.X; y = tplist[ 0 ].Location.Y; }
index++;
}
List-tplist;
整数指数;
int x,y;
私有无效按钮\u追加\u单击(对象发送方,事件参数e)
{
日期时间选择器;
tp=新的日期时间选择器();
tp.Format=DateTimePickerFormat.Custom;
tp.CustomFormat=“mm:ss”;
tp.ShowUpDown=true;
tp.Value=DateTime.Now.Date;
tp.Value=新的日期时间(2000,1,1,0,索引,0);
dataGridView1.Controls.Add(tp);
dataGridView1.Rows.Add(新对象[]{“行”+
转换.ToString(索引),
“val”+Convert.ToString(index)});
矩形框=dataGridView1.GetCellDisplayRectangle(1,
dataGridView1.RowCount-1,false);
tp.尺寸=框架尺寸;
tp.位置=帧位置;
如果(dataGridView1.RowCount)请共享您的代码,了解代码并提及您想要实现的目标将很有帮助。从描述中,我猜,您希望调整GridView的大小并保持其在下次运行时的状态。这就是您想要的吗?
private void Adjust()
{
for( int i = 0; i < tplist.Count; i++ )
{
tplist[ i ].Location = dataGridView1.GetCellDisplayRectangle( 1, i, true ).Location;
if( tplist[ i ].Location.X == 0 )
{
tplist[ i ].Location = new Point( x, y );
}
else
{
x = tplist[ 0 ].Location.X; y = tplist[ 0 ].Location.Y;
}
tplist[ i ].Size = dataGridView1.GetCellDisplayRectangle( 1, i, false ).Size;
if( i >= dataGridView1.FirstDisplayedCell.RowIndex && i < dataGridView1.FirstDisplayedCell.RowIndex + dataGridView1.DisplayedRowCount( true ) )
tplist[ i ].Visible = true;
else tplist[ i ].Visible = false;
}
dataGridView1.Refresh();
}