2017-02-12 38 views
0

我有,當我點擊其中一個窗體上的DataGridView,給了我正確的行/列索引點擊:上驗證[第三 enter image description here如何得到正確的datagridview的有效行/列索引的MdiParent在C#

點擊列,第一行,給出了上述]。 但是,當這種形式是從MDI父叫,點擊thesame列三,排一(驗證)給出了這樣的: enter image description here

下面是形式背後代碼:

var result = new DAO().RetrieveAllProjects().Select(r => new { r.Name }).ToList(); 

dataGridView1.DataSource = result; 
DataGridViewButtonColumn btn = new DataGridViewButtonColumn(); 
btn.HeaderText = ""; 
btn.Text = "View/Edit"; 
btn.Name = "edit"; 
btn.UseColumnTextForButtonValue = true; 

DataGridViewButtonColumn btn2 = new DataGridViewButtonColumn(); 
btn2.HeaderText = ""; 
btn2.Text = "Verify"; 
btn2.Name = "verify"; 
btn2.UseColumnTextForButtonValue = true; 

dataGridView1.Columns.Add(btn); 
dataGridView1.Columns.Add(btn2); 

dataGridView1.Columns[0].Width = 290; 
dataGridView1.AutoSize = true; 
dataGridView1.CellClick += dataGridView1_CellClick; 

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 
{ 
    MessageBox.Show(string.Format("ColumnIndex:{0}, RowIndex: {1}", e.ColumnIndex, e.RowIndex));   
} 

而且它的從MDI父稱這種方式:

projectsListForm pLF = new projectsListForm(); 
pLF.MdiParent = this; 
pLF.Show(); 

我怎樣才能確保正確的行/列在MDI父強調

+0

是有一些原因,你必須設置父形式一個'MdiParent',因爲這似乎是子表單中列索引的問題。爲什麼索引是不正確的,我不知道。 – JohnG

回答

1

出於某種原因[s]使用MdiParent時,dataGridView1中會發生兩輪數據綁定(觸發DataGridView DataBindingComplete事件)。 「名稱」列不必要的刪除和添加(DataGridView ColumnAddedColumnRemoved事件)。爲了塞汀數據源後,我禁用列自動生成停止這種行爲:

dataGridView1.DataSource = ...; 
dataGridView1.AutoGenerateColumns = false; 

一個更安全的選擇是通過名稱的列的工作,而不是指數

相關問題