我've come across something odd in my c#-4.0 Windows Form application and I'我不确定是什么原因造成的 . 基本上,我有一个带有DataGridView和一些文本框的表单,在我的网格中,我有一个离开事件,当用户离开DataGridView时选择 Rows[0].Cells[0]
.
现在,如果我单击网格中的单元格,编辑单元格并直接单击文本框中的leave事件正确触发并选择行/单元格[0],但此时表单变得无法响应 .
如何使用Visual Studio进行复制(我正在使用2010专业版)
-
创建新的WindowsFormApplication
-
将DataGridView和TextBox添加到表单 .
-
现在,在Form1_Load事件中添加以下代码:
private void Form1_Load(object sender, EventArgs e)
{
DataTable dtTmp = new DataTable("temp");
dtTmp.Columns.Add("col 1", typeof(String));
dtTmp.Columns.Add("col 2", typeof(String));
DataSet dsTmp = new DataSet();
dsTmp.Tables.Add(dtTmp);
DataRow dr1 = dsTmp.Tables["temp"].NewRow();
dr1["col 1"] = "aaa";
dr1["col 2"] = "12";
dsTmp.Tables["temp"].Rows.Add(dr1);
DataRow dr2 = dsTmp.Tables["temp"].NewRow();
dr2["col 1"] = "bbb";
dr2["col 2"] = "1234";
dsTmp.Tables["temp"].Rows.Add(dr2);
dataGridView1.DataSource = dsTmp;
dataGridView1.DataMember = "temp";
dataGridView1.Refresh();
}
接下来,为DataGridView1创建一个Leave事件并添加以下代码:
private void dataGridView1_Leave(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count > 0)
{
dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0];
}
}
调试并执行以下步骤:
-
单击第1列中包含"bbb"的第二行中的单元格 .
-
在该单元格中输入其他内容 .
-
Without hitting enter, space, tab, down or right arrow ,单击您添加到表单的文本框 .
现在尝试关闭表单,它不会关闭 .
我的 dataGridView1.CurrentCell
线有什么问题?如果您选择并编辑第一行,则表单会正常关闭,但如果是第二行则不会 .
1 回答
不确定它是如何干扰的,但是Leave事件正在干扰某些事情 . 通常我的治疗方法是尝试在Leave事件后运行代码: