datagridview 中TextBox列。输入自动提示。
C# winform datagridview 中TextBox列。输入自动提示。并且禁止输入数字。请高手指教。
------解决方案--------------------private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty;
if(dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "A")
{
Regex rx = new Regex(@"^[1-9]\d*$", RegexOptions.Compiled);
if (rx.IsMatch(e.FormattedValue.ToString()))
{ dataGridView1.Rows[e.RowIndex].ErrorText = "输入有误!";
e.Cancel = true; }
}
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty;
}
------解决方案--------------------给你个比较好用的
C# code
public DataGridViewTextBoxEditingControl CellEdit = null;
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
CellEdit = (DataGridViewTextBoxEditingControl)e.Control;
CellEdit.ShortcutsEnabled = false;
CellEdit.KeyPress += new KeyPressEventHandler(CellEdit_KeyPress);
}
private void CellEdit_KeyPress(object sender, KeyPressEventArgs e)
{
if (Char.IsNumber(e.KeyChar))
{
e.Handled = true;
MessageBox.show("Num forbiden");
}
}