将文本框放在 DataGridView 中的标题列的顶部

本文关键字:标题 顶部 DataGridView 文本 | 更新日期: 2023-09-27 18:34:40

我有以下函数来响应 DataGridView 中的 ColumnHeaderMouseDoubleClick 事件:

void grid_ColumnHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
    TextBox t = new TextBox();
    //make the new textbox the same size as the header to cover it up until text is entered
    t.Width = ((DataGridView)sender).CurrentCell.ContentBounds.Width;
    t.Height = ((DataGridView)sender).CurrentCell.ContentBounds.Height;
    t.Dock = DockStyle.Fill;
    t.Visible = true;
    t.BringToFront();
    t.Text = "TEST";
    Controls.Add(t);
}

所有这些代码都发生在一个类中,该类扩展了 Panel,并将 DataGridView 添加到面板的控件中。当我双击标题并在此处理程序上放置断点时,将调用处理程序,但我在任何地方都看不到文本框。有人知道我做错了什么吗?

将文本框放在 DataGridView 中的标题列的顶部

我会首先将文本框设置为类字段(在事件触发器之外声明它,这样它就不会在结束时超出范围(。 可能还必须设置文本框的坐标以及:)。这台机器上没有 VS 来尝试一下,但我希望这会有所帮助。