DataGridView:编辑时更改编辑控件大小 [英] DataGridView: Change Edit Control size while editing
问题描述
在 DataGridView 中,当我编辑单元格时,我希望单元格大小根据字符串长度进行扩展.Excel 也是如此.
in the DataGridView I want the cell size to expand according to the string length when I edit the cell. Excel does the same.
在DataGridView中,当进入编辑模式时,在单元格位置放置一个DataGridViewTextBoxEditingControl.我试图改变这个控件的边界/大小,但结果只是我想要的大小的短暂闪烁.它会直接覆盖原始的截断方式.
In the DataGridView, when entering edit mode, a DataGridViewTextBoxEditingControl is placed at the cell position. I tried to change the bounds/size of this control, but result is just a short flicker of my desired size. It gets directly overpainted the original, truncated way.
关于如何让它工作的任何想法?
Any ideas on how to get this working?
谢谢,
蒂莫
推荐答案
这个问题已经很老了,但希望我的回答能帮助到后来的人.我遇到了同样的问题,并且能够使用类似于以下的过程在用户键入时动态更新列宽,以确保文本适合列.
This question is quite old but hopefully my answer helps somebody down the road. I ran across the same problem and was able to use a process similar to the following to make the column width update dynamically as the user typed, in order to ensure the text fit in the column.
使用的事件:
CellBeginEdit
CellEndEdit
EditingControlShowing
TextBoxKeyPressEvent
(即KeyPress
)
注意:以下代码假设 AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
// ---------------------------------------------------------------------------
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
// Copies the original column width because switching to DataGridViewAutoSizeColumnMode.None
// will automatically make the column a default width.
int origColumnWidth = dataGridView1.Columns[e.ColumnIndex].Width;
dataGridView1.Columns[e.ColumnIndex].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
// Reverts back to the original width.
dataGridView1.Columns[e.ColumnIndex].Width = origColumnWidth;
}
// ---------------------------------------------------------------------------
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Columns[e.ColumnIndex].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}
// ---------------------------------------------------------------------------
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is TextBox)
{
var tbox = (e.Control as TextBox);
// De-register the event FIRST so as to avoid multiple assignments (necessary to do this or the event
// will be called +1 more time each time it's called).
tbox.KeyPress -= TextBoxKeyPressEvent;
tbox.KeyPress += TextBoxKeyPressEvent;
}
}
// ---------------------------------------------------------------------------
private void TextBoxKeyPressEvent(object sender, KeyPressEventArgs e)
{
// Gets the text prior to the new character being added. Appending an arbitrary "0" to the value
// to account for the missing character when determining appropriate measurements.
string prevText = dataGridView1.CurrentCell.EditedFormattedValue.ToString() + "0";
Graphics editControlGraphics = dataGridView1.EditingControl.CreateGraphics();
// Gets the length of the current text value.
SizeF stringSize = editControlGraphics.MeasureString(prevText, dataGridView1.EditingControl.Font);
int widthForString = (int)Math.Round(stringSize.Width, 0);
// Makes the column width big enough if it's not already.
if (dataGridView1.CurrentCell.OwningColumn.Width < widthForString)
{
dataGridView1.CurrentCell.OwningColumn.Width = widthForString;
}
}
更新 TextBoxKeyPressEvent
逻辑以考虑退格:
Update to the TextBoxKeyPressEvent
logic to account for Backspace:
private void TextBoxKeyPressEvent(object sender, KeyPressEventArgs e)
{
string prevText;
bool wasBackspaced = false;
// The following logic will either add or remove a character to/from the text string depending if the user typed
// an additional character or pressed the Backspace key. At the end of the day, the cell will (at least) be
// sized to the configured minimum column width or the largest row width in the column because we're using
// AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells.
if (e.KeyChar == Convert.ToChar(Keys.Back))
{
prevText = dataGridView1.CurrentCell.EditedFormattedValue.ToString();
if (prevText.Length == 0)
{
// Don't try to make it any smaller...
return;
}
// Remove an arbitrary character for determining appropriate measurements.
prevText = prevText.Remove(prevText.Length - 1);
wasBackspaced = true;
}
else
{
// Gets the text prior to the new character being added. Appending an arbitrary "0" to the value
// to account for the missing character when determining appropriate measurements.
prevText = dataGridView1.CurrentCell.EditedFormattedValue.ToString() + "0";
}
Graphics editControlGraphics = dataGridView1.EditingControl.CreateGraphics();
// Gets the length of the current text value.
SizeF stringSize = editControlGraphics.MeasureString(prevText, dataGridView1.EditingControl.Font);
int widthForString = (int)Math.Round(stringSize.Width, 0);
// Makes the column width big, or small, enough if it's not already.
if (dataGridView1.CurrentCell.OwningColumn.Width < widthForString || // 1. Applies when adding text
(dataGridView1.CurrentCell.OwningColumn.Width > widthForString && // ---
dataGridView1.CurrentCell.OwningColumn.MinimumWidth < widthForString && // 2. Applies when backspacing
wasBackspaced)) // ---
{
dataGridView1.CurrentCell.OwningColumn.Width = widthForString;
}
}
这篇关于DataGridView:编辑时更改编辑控件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!