DataGridView 编辑列的问题(我的可用分只剩下这么多了)

lxl_sprots 2010-07-27 06:52:03
DataGridView 编辑列
我在绑定的时候 里面绑定的数据是 decimal(18,2) 类型的,当我 编辑的时候 如果输入了 字符串那么就会报错,那么我想控制一下单元格 怎么能不让输入 字符串。。。

出现的情况是:当我在DataGridView 的单元格里 输入一个字符串 当鼠标离开的时候 就直接报错了。。。这个怎么控制啊

由于我的可用分数太少 希望大家能帮帮我 。。。谢谢了

...全文
85 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mill_dhl 2010-07-28
  • 打赏
  • 举报
回复
有个数字控件可以用的,此控件不能输入非数字
平生我自如 2010-07-28
  • 打赏
  • 举报
回复
我一直用的这种方法[Quote=引用 3 楼 dingxinghui 的回复:]
C# code

前提建数据表绑定DataGridView时,要定义各列的数据类型
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{

if (e.Exception != null)
{
……
[/Quote]
dingxinghui 2010-07-27
  • 打赏
  • 举报
回复

前提建数据表绑定DataGridView时,要定义各列的数据类型
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{

if (e.Exception != null)
{
DataGridView dgv = (DataGridView)sender;

if (e.Context == DataGridViewDataErrorContexts.Commit)
{
MessageBox.Show("输入的数量格式不正确。");
}
if (e.Context == DataGridViewDataErrorContexts.CurrentCellChange)
{
MessageBox.Show(e.Exception.Message);
}
if (e.Context == DataGridViewDataErrorContexts.Parsing)
{
MessageBox.Show(e.Exception.Message);
}
if (e.Context == DataGridViewDataErrorContexts.LeaveControl)
{
MessageBox.Show(e.Exception.Message);
}

if ((e.Exception) is ConstraintException)
{
DataGridView view = (DataGridView)sender;
view.Rows[e.RowIndex].ErrorText = e.Exception.Message; ;
view.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = e.Exception.Message;
e.ThrowException = false;
}

dgv.CancelEdit();
}
}
醒子宇 2010-07-27
  • 打赏
  • 举报
回复
楼上的可以
wuyq11 2010-07-27
  • 打赏
  • 举报
回复
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
double d= 0.0;
dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty;
if(dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "")
{
if(!double.TryParse(e.FormattedValue.ToString(),out d) )
{
dataGridView1.Rows[e.RowIndex].ErrorText = "输入有误!";
e.Cancel = true;
}
}
}

110,536

社区成员

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

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

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