C# Winform中DataGridView问题 怎样获取DataGridView单元格中输入的数据(注:是单元格编辑未完成前的数据) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 因为我在dgv_M_CellLeave事件中获取的值是空的在dgv_M_CellEndEdit事件中可以获得,但这时系统已经报告用户录入的数据不符合类型,系统提示的内容他们根本看不明白。 DataGridView的编辑框Cell里,有一个EditingTextBox私有属性(DataGridViewTextBoxEditingControl类型,继承自TextBox,直接强转为TextBox即可),利用反射得到其实例,给Textchange事件绑上处理函数~~然后就可以了 TextBox cell;//初始化一个文本框 private void dgv_M_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { cell = new TextBox(); cell = (TextBox)e.Control;//把当前编辑的单元格转换成文本框 cell.KeyPress += new KeyPressEventHandler(cell_KeyPress); } private void cell_KeyPress(object sender, KeyPressEventArgs e) { //这里判断输入的每个字符 } private void dataGridView1_CellFormatting(object sender,System.Windows.Forms.DataGridViewCellFormattingEventArgs e) { if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("A")) { Int32 intValue; if (Int32.TryParse((String)e.Value, out intValue) && (intValue < 0)) { e.CellStyle.BackColor = Color.Red; e.CellStyle.SelectionBackColor = Color.DarkRed; } }}单元格的验证可以使用dgv_details_CellValidating事件。验证不通过时调用e.Cancel = true;终止事件链,单元格将保持编辑状态。调用dgv_details.CancelEdit();可以使单元格的内容会滚到修改前的值。使用System.Windows.Forms.SendKeys.Send("^a");将全选单元格的内容。 如何为按钮设置位置 连接数据库好像有问题。。这个什么问题来的 如何让窗口最小化后还照样运行,但不自动跳出来成为当前工作窗口 谁能给我一些c#编程题目 谢谢!!! A窗体继承B窗体,为何A上的原有控件被覆盖? WinForm 加载Flash出现异常 有了XSD,如何通过XSD读取XML数据? 使用VPN后SQL数据库插入数据速度慢 [C#]拖动窗体或按住某些窗体控件时,计算工作暂停... 水晶报表的到处功能 (200分) 共享传文件---需要验证身份- C# 控制台的一段小代码改为应用程序的代码
在dgv_M_CellEndEdit事件中可以获得,但这时系统已经报告用户录入的数据不符合类型,系统提示的内容他们根本看不明白。
TextBox cell;//初始化一个文本框
private void dgv_M_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
cell = new TextBox();
cell = (TextBox)e.Control;//把当前编辑的单元格转换成文本框
cell.KeyPress += new KeyPressEventHandler(cell_KeyPress);
} private void cell_KeyPress(object sender, KeyPressEventArgs e)
{
//这里判断输入的每个字符
}
System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("A"))
{
Int32 intValue;
if (Int32.TryParse((String)e.Value, out intValue) &&
(intValue < 0))
{
e.CellStyle.BackColor = Color.Red;
e.CellStyle.SelectionBackColor = Color.DarkRed;
}
}
}
单元格的验证可以使用dgv_details_CellValidating事件。
验证不通过时调用e.Cancel = true;终止事件链,单元格将保持编辑状态。
调用dgv_details.CancelEdit();可以使单元格的内容会滚到修改前的值。
使用System.Windows.Forms.SendKeys.Send("^a");将全选单元格的内容。