如何控制在DataGrid中不能输入全角数字,符号,字母?
===============================================
1.屏蔽全角
2.把全角转为半角
private string ToDBS(string str)//全角转半角
{
return Regex.Replace(str,"[\\w]",new MatchEvaluator(RegReplace));
}
private string RegReplace(Match m)
{
if((int)m.Value[0]>=65281&(int)m.Value[0]<=65374)return ((char)((int)m.Value[0]-65248)).ToString();
if((int)m.Value[0]==12288)return ((char)32).ToString();;
return m.Value;
}
Top
/// 半角转全角
/// </summary>
/// <param name="BJstr"></param>
/// <returns></returns>
static public string GetQuanJiao(string BJstr)
{
#region
char[] c = BJstr.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
if (b.Length == 2)
{
if (b[1] == 0)
{
b[0] = (byte)(b[0] - 32);
b[1] = 255;
c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
}
}
}
string strNew = new string(c);
return strNew;
#endregion
}
/// <summary>
/// 全角转半角
/// </summary>
/// <param name="QJstr"></param>
/// <returns></returns>
static public string GetBanJiao(string QJstr)
{
#region
char[] c = QJstr.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
if (b.Length == 2)
{
if (b[1] == 255)
{
b[0] = (byte)(b[0] + 32);
b[1] = 0;
c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
}
}
}
string strNew = new string(c);
return strNew;
#endregion
}