我對DataGridView沒有什麼問題。
1.形式刪除DataGridView控件和設置屬性可見的假
2.添加幾行並更改可見真像上面的代碼。防止在DataGridView中選擇單元格
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Rows.Add(new object[] { "1", "a" });
dataGridView1.Rows.Add(new object[] { "2", "b" });
dataGridView1.Rows.Add(new object[] { "3", "c" });
dataGridView1.Rows.Add(new object[] { "4", "d" });
dataGridView1.Visible = true;
//^ this trigger selection
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
Console.WriteLine("selected");
}
設置後可見爲True,第一個單元格被自動選中並觸發SelectionChanged事件。
如何防止?
編輯。解決方案:
- 分離事件處理程序:
- 設置可見
- 清除選擇
- 添加處理器
dataGridView1.SelectionChanged -= dataGridView1_SelectionChanged;
dataGridView1.Visible = true;
dataGridView1.ClearSelection();
dataGridView1.SelectionChanged += dataGridView1_SelectionChanged;
我認爲你的問題是,'dataGridView1_SelectionChanged'被解僱,因爲這行代碼:'dataGridView1.Visible = TRUE;'。設置網格的可見性將觸發事件。爲什麼你想在開始時將網格設置爲false? – KaeL
我想在特定情況下顯示網格。在開始時,網格必須隱藏。 – revelvice
我的帖子中的解決方案。 – revelvice