c#中字符转换问题

sione88 2007-11-22 06:19:39
请问各位,我在Datagridviiew中输入字母时,无论我输入的是小写还是汉字编辑状态下的字母,都转换为大写的字母,怎么写代码?
例如:我要输入"A-A-1",当我输入"a-a-1"自动变为"A-A-1"?请赐教!谢谢!
...全文
98 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
boblaw 2007-11-22
  • 打赏
  • 举报
回复
楼上的方法果然不错
王集鹄 2007-11-22
  • 打赏
  • 举报
回复
发现有个EditingControlShowing可以得到编辑器的实例,参考如下代码实现
private void dataGridView1_EditingControlShowing(object sender, 
DataGridViewEditingControlShowingEventArgs e)
{
Console.WriteLine("dataGridView1_EditingControlShowing");
if (e.Control is DataGridViewTextBoxEditingControl)
{
if (((DataGridView)sender).CurrentCell.ColumnIndex == 0) // 第一列大写
{
((DataGridViewTextBoxEditingControl)e.Control).CharacterCasing =
CharacterCasing.Upper;
}
else
{
((DataGridViewTextBoxEditingControl)e.Control).CharacterCasing =
CharacterCasing.Normal;
}
}
}

boblaw 2007-11-22
  • 打赏
  • 举报
回复
应该是没有办法了,即使使用API也不行,因为获取不到Cell的句柄
sione88 2007-11-22
  • 打赏
  • 举报
回复
楼上的方法我试过了,还是可以的,但是我不是在CELL完成后,是在我输入小字的时候就直接变成大写,这样可以吗?
boblaw 2007-11-22
  • 打赏
  • 举报
回复
使用CellFormatting事件,更为合理

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name=="col" && e.Value!=null)
e.Value = e.Value.ToString().ToUpper();
}
vefo 2007-11-22
  • 打赏
  • 举报
回复
ls
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex].Value = ((DataGridView)sender).Rows[e.RowIndex].Cells[ e.ColumnIndex].Value.ToString().ToUpper(); }
王集鹄 2007-11-22
  • 打赏
  • 举报
回复
编辑结束后全部大写化
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex].Value =
((DataGridView)sender).Rows[e.RowIndex].Cells[
e.ColumnIndex].Value.ToString().ToUpper();
}
Code従業員 2007-11-22
  • 打赏
  • 举报
回复
Datagridviiew属性旁边就有个闪电样的图标,在那里加KeyPress事件
Code従業員 2007-11-22
  • 打赏
  • 举报
回复
这个是事件的方法,上面所用的e是获取的按键(就是事件激活的时候带的参数),KeyChar是把它转为字符的方法.
sione88 2007-11-22
  • 打赏
  • 举报
回复
不过,在Datagridviiew里没有KeyChar这个属性哦,该怎么写呢?
sione88 2007-11-22
  • 打赏
  • 举报
回复
楼上的谢谢了,我先试一下看看!
Code従業員 2007-11-22
  • 打赏
  • 举报
回复
在TEXTBOX是这样的,添加KeyPress事件,里面加上

if (Char.IsLower(e.KeyChar))
{
e.KeyChar = Char.ToUpper(e.KeyChar);
}

然而在Datagridviiew里面KeyPress事件没有明显的反映出按键的值,不好弄.

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧