我知道如何設置datagridview的特定列中的行的最大長度,但是如果輸入更短的長度的字符串,它每次都會更改。我想要設置長度使得最大長度一開始只設置一次,這基本上是字符串的長度。第一次設置特定列的行的最大長度
例如,如果該字符串的長度爲5在如此,即使我改變字符串的文字和長度被改變爲3的最大長度的開始保持5.
這是我的代碼。
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
//check if currently selected cell is cell you want
if (dataGridView1.CurrentCell == null || dataGridView1.CurrentCell.ColumnIndex != 2)
{
return;
}
if (e.Control is TextBox && !(Convert.ToBoolean(this.dataGridView1.CurrentRow.Cells[8].Value.ToString())))
{
((TextBox)e.Control).MaxLength = Convert.ToInt16(this.dataGridView1.CurrentRow.Cells[3].Value.ToString());
}
}
創建一個布爾變量VAR isFirstTime =真正;那麼在你的代碼中檢查if(isFirstTime)並設置你的最大長度並將此參數改爲false。 – Ehsan