Datagridview 一些列只接受字符串、数字和一些特定格式
Datagridview some columns accept only string , numeric and and some specific format
我正在处理 Datagridview,我有多个列。有些列必须接受唯一的数值,有些只接受字符串,有些列接受特定格式,如 0000-0000-0000。我使用了以下代码:
private void adgUser_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (adgUser.CurrentCell.ColumnIndex == 0)
{
e.Control.KeyPress -= AllowLettersOnly;
e.Control.KeyPress += AllowLettersOnly;
}
if (adgUser.CurrentCell.ColumnIndex == 1)
{
e.Control.KeyPress -= AllowLettersOnly;
e.Control.KeyPress += AllowLettersOnly;
}
if (adgUser.CurrentCell.ColumnIndex == 2)
{
e.Control.KeyPress -= AllowNumbersOnly;
e.Control.KeyPress += AllowNumbersOnly;
}
}
private void AllowNumbersOnly(Object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
e.Handled = true;
}
private void AllowLettersOnly(Object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar) && !char.IsWhiteSpace(e.KeyChar))
e.Handled = true;
}
现在,这段代码不能正常工作。我正在通过 datagridview 将数据插入数据库 table 并确保列在插入数据时只接受有效格式。请指教
我怀疑您没有将 KeyPress
处理程序分配给隐藏在 DataGrdivView
中的 TexBox
控件,环顾四周发现了这个 ,将其应用于您的示例,它按预期工作:
private void adgUser_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox txtEdit = e.Control;
txtEdit.MaxLength = 2;
// remove any existing handler
txtEdit.KeyPress -= txtdgvDiaryEdit_Keypress;
txtEdit.KeyPress += txtdgvDiaryEdit_Keypress;
}
private void txtdgvDiaryEdit_Keypress(object sender, Global.System.Windows.Forms.KeyPressEventArgs e)
{
// Test for numeric value or backspace
if (Information.IsNumeric(e.KeyChar.ToString()) | e.KeyChar == (char)Keys.Back)
{
e.Handled = false; // if numeric
}
else
{
e.Handled = true;
} // if non numeric
}
这只允许两位数。显然,您必须应用您的约束(数字、字母、长度...),但我相信逻辑就是您所追求的。
我正在处理 Datagridview,我有多个列。有些列必须接受唯一的数值,有些只接受字符串,有些列接受特定格式,如 0000-0000-0000。我使用了以下代码:
private void adgUser_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (adgUser.CurrentCell.ColumnIndex == 0)
{
e.Control.KeyPress -= AllowLettersOnly;
e.Control.KeyPress += AllowLettersOnly;
}
if (adgUser.CurrentCell.ColumnIndex == 1)
{
e.Control.KeyPress -= AllowLettersOnly;
e.Control.KeyPress += AllowLettersOnly;
}
if (adgUser.CurrentCell.ColumnIndex == 2)
{
e.Control.KeyPress -= AllowNumbersOnly;
e.Control.KeyPress += AllowNumbersOnly;
}
}
private void AllowNumbersOnly(Object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
e.Handled = true;
}
private void AllowLettersOnly(Object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar) && !char.IsWhiteSpace(e.KeyChar))
e.Handled = true;
}
现在,这段代码不能正常工作。我正在通过 datagridview 将数据插入数据库 table 并确保列在插入数据时只接受有效格式。请指教
我怀疑您没有将 KeyPress
处理程序分配给隐藏在 DataGrdivView
中的 TexBox
控件,环顾四周发现了这个
private void adgUser_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox txtEdit = e.Control;
txtEdit.MaxLength = 2;
// remove any existing handler
txtEdit.KeyPress -= txtdgvDiaryEdit_Keypress;
txtEdit.KeyPress += txtdgvDiaryEdit_Keypress;
}
private void txtdgvDiaryEdit_Keypress(object sender, Global.System.Windows.Forms.KeyPressEventArgs e)
{
// Test for numeric value or backspace
if (Information.IsNumeric(e.KeyChar.ToString()) | e.KeyChar == (char)Keys.Back)
{
e.Handled = false; // if numeric
}
else
{
e.Handled = true;
} // if non numeric
}
这只允许两位数。显然,您必须应用您的约束(数字、字母、长度...),但我相信逻辑就是您所追求的。