[英]C#: How to validate datagridview cells?
我有一個datagridview,我只允許用戶輸入數字。但是如果用戶錯誤輸入字母,那么我會顯示錯誤消息。但是問題出在我單擊消息框上的“確定”按鈕后,我想清除該特定的單元格值並允許用戶輸入其他值。我該怎么做?
我應該使用哪個事件或功能。
提前致謝....
獲取行索引,並從行索引獲取單元格索引。 這樣你就可以改變價值
只是一個想法,我知道這是一個老問題,但是我要做的是將單元格限制為只接受數字。 為此,我重寫了ProcessCmdKey方法。 這是我使用的一些代碼,它應該對您有用,不要忘記將Gridname從dgListData更改為您的網格名稱:
/// <summary>
/// The purpose of this method is two fold. First it stops the data grid
/// from adding a new row when the enter key is pressed during editing a cell
/// Secondly it filters the keys for numeric characters only when the selected
/// cell type is of a numeric type.
/// </summary>
/// <param name="msg"></param>
/// <param name="keyData"></param>
/// <returns></returns>
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
//Let's get fancy and detect the column type and then filter the keys as needed.
if (!dgListData.IsCurrentCellInEditMode) return base.ProcessCmdKey(ref msg, keyData); //only go into the routine if we are in edit mode
if (helper.IsNumericType(dgListData.CurrentCell.ValueType))
{
if (helper.NumericFilterKeyData(keyData)) return true; //override the key
}
// Check if Enter is pressed
if (keyData != Keys.Enter) return base.ProcessCmdKey(ref msg, keyData);
// If there isn't any selected row, do nothing
SetFocusToNextVisibleColumn(dgListData);
return true;
}
private void SetFocusToNextVisibleColumn(DataGridView dgViewTarget)
{
if (dgViewTarget.CurrentCell == null) return;
Console.WriteLine(dgViewTarget.CurrentCell.Value);
if (dgViewTarget.CurrentCell.IsInEditMode) dgViewTarget.EndEdit();
var currentColumn = dgViewTarget.CurrentCell.ColumnIndex;
if (currentColumn < dgViewTarget.ColumnCount) ++currentColumn;
for (var i = currentColumn; i < dgViewTarget.ColumnCount; i++)
{
if (!dgViewTarget[i, dgViewTarget.CurrentRow.Index].Displayed) continue;
dgViewTarget.CurrentCell = dgViewTarget[i, dgViewTarget.CurrentRow.Index];
break;
}
}
我還有一個靜態幫助器類,在我的項目中會用到,這是您需要的兩個例程。 只需添加一個新的helper.cs類並將此代碼粘貼到其中即可。
public static class helper
{
private static readonly KeysConverter Kc = new KeysConverter();
/// <summary>
/// Filters the keys for numeric entry keys
/// </summary>
/// <param name="keyData"></param>
/// <returns></returns>
public static bool NumericFilterKeyData(Keys keyData)
{
var keyChar = Kc.ConvertToString(keyData);
return !char.IsDigit(keyChar[0])
&& keyData != Keys.Decimal
&& keyData != Keys.Delete
&& keyData != Keys.Tab
&& keyData != Keys.Back
&& keyData != Keys.Enter
&& keyData != Keys.OemPeriod
&& keyData != Keys.NumPad0
&& keyData != Keys.NumPad1
&& keyData != Keys.NumPad2
&& keyData != Keys.NumPad3
&& keyData != Keys.NumPad4
&& keyData != Keys.NumPad5
&& keyData != Keys.NumPad6
&& keyData != Keys.NumPad7
&& keyData != Keys.NumPad8
&& keyData != Keys.NumPad9;
}
/// <summary>
/// Determines if a type is numeric. Nullable numeric types are considered numeric.
/// </summary>
/// <remarks>
/// Boolean is not considered numeric.
/// </remarks>
public static bool IsNumericType(Type type)
{
if (type == null)
{
return false;
}
switch (Type.GetTypeCode(type))
{
case TypeCode.Byte:
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.SByte:
case TypeCode.Single:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
return true;
case TypeCode.Object:
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return IsNumericType(Nullable.GetUnderlyingType(type));
}
return false;
}
return false;
}
/// <summary>
/// Tests if the Object is of a numeric type This is different than testing for NumericType
/// as this expects an object that contains an expression like a sting "123"
/// </summary>
/// <param name="expression">Object to test</param>
/// <returns>true if it is numeric</returns>
public static bool IsNumeric(Object expression)
{
if (expression == null || expression is DateTime)
return false;
if (expression is Int16 || expression is Int32 || expression is Int64 || expression is Decimal ||
expression is Single || expression is Double || expression is Boolean)
return true;
try
{
if (expression is string)
{
if (
expression.ToString()
.IndexOfAny(
@" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOOPQRSTUVWXYZ!@#$%^&*?<>[]|+-*/\'`~_"
.ToCharArray()) != -1) return false;
}
else
{
Double.Parse(expression.ToString());
}
return true;
}
catch
{
} // just dismiss errors but return false
return false;
}
}
希望以后對您有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.