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
)
CellBeginEdit
CellEndEdit
EditingControlShowing
TextBoxKeyPressEvent
(i.e.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屋!