怎么样实现DataGridView的cell中内容出现错误后,自动重新恢复到编辑状态! 如题,小弟遇到这样一个难题:某个cell编辑状态结束后,判断数据出错,想使之重新恢复到编辑状态,而其他都不处于选中状态。用cell.Selected=true,效果不好啊。哪位大侠有更好的办法吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 this.dataGridView1.BeginEdit(false); 我是这样做的,但是效果怎么是焦点跑到下面的cell上了,呵呵! 你要先设置当然选中的单元格this.dataGridView1.CurrentCell = this.dataGridView1.Rows[0].Cells[0]; 还是不行啊。当数据错误后,我弹出一个对话框,点击“确定”后,下一个单元格的内容变成了空。这是怎么回事啊?如果按Enter键,焦点还是下面,是不是要接受Enter的值啊! this.dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; DataGridView dgv = (DataGridView)(sender); DataGridViewCell cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex]; int value = (int)(cell.Value); if (value < 0 || value > 100) { MessageBox.Show(this, "政治分数必须介于0到100之间!", "数据出界!", MessageBoxButtons.OK, MessageBoxIcon.Error); this.dataGridView1.CurrentCell = cell; cell.Value = 0; this.dataGridView1.BeginEdit(false); }多谢兄弟了,呵呵!在线等! CellValidating事件里面写验证如: private void GV_PriceInfo_CellValidating(object sender, DataGridViewCellValidatingEventArgs e){ if (!String.IsNullOrEmpty(e.FormattedValue.ToString())) { String str = @"^(?:[0-9][0-9]*(?:\.[0-9]+)?|0(?:\.[0-9]+)?)$"; if (!System.Text.RegularExpressions.Regex.IsMatch(e.FormattedValue.ToString(), str)) { MessageBox.Show("输入的字符串格式不正确"); GV_PriceInfo.CancelEdit(); e.Cancel = true; } }} 谢谢大哥,你的方法很好!不过又有了一个新问题,就是DataGridView启动后,默认的是第一行第一个单元格,结果验证了这个cell。而我想验证的仅仅是第四列,这怎么实现呢?多谢大哥赐教!我的QQ是:63292754.能否加兄弟为好友? 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) || d<0.0) { dataGridView1.Rows[e.RowIndex].ErrorText = "数量输入有误!"; e.Cancel = true; } } } private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty; } http://www.cnblogs.com/freeliver54/articles/903850.html 我又回来了,继续抉择,兄弟们给参考下 winform中的keydown 求问关于连接accsee数据库的问题 help问个小问题 ado里面有没有类似asp中 使用asp_regsql然后继续做登录的文件? 谁用过BindSource? 求一段分秒倒计时的代码 一个DataGrid分页后查询的问题 c#编写个人理财系统 pictureBox的Image属性赋值后怎样取消? C#爱好者求助! 如何使用代码让用户登陆? 未将对象引用设置到对象的实例 SOCKET
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[0].Cells[0];
DataGridViewCell cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
int value = (int)(cell.Value);
if (value < 0 || value > 100)
{
MessageBox.Show(this, "政治分数必须介于0到100之间!",
"数据出界!",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
this.dataGridView1.CurrentCell = cell;
cell.Value = 0;
this.dataGridView1.BeginEdit(false);
}多谢兄弟了,呵呵!在线等!
如: private void GV_PriceInfo_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (!String.IsNullOrEmpty(e.FormattedValue.ToString()))
{
String str = @"^(?:[0-9][0-9]*(?:\.[0-9]+)?|0(?:\.[0-9]+)?)$";
if (!System.Text.RegularExpressions.Regex.IsMatch(e.FormattedValue.ToString(), str))
{
MessageBox.Show("输入的字符串格式不正确");
GV_PriceInfo.CancelEdit();
e.Cancel = true;
}
}
}
{
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) || d<0.0)
{
dataGridView1.Rows[e.RowIndex].ErrorText = "数量输入有误!";
e.Cancel = true;
}
}
} private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty;
}
http://www.cnblogs.com/freeliver54/articles/903850.html