2016-06-28 75 views
0

enter image description hereC# - 如何獲取組合框中值更改事件中每行的值?

海大家。對於連續的每一列,我如何獲得組合框值改變事件上的所有值。它可以在「目標」或「數據類型」列中的組合框中。我覺得像在文本列下面的例子:

  string Source = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); 
      string Destination = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString(); 
      string Similarity = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString(); 
      string Datatype = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString(); 

示例代碼或建議的最佳方式可能是這個高度讚賞。先謝謝你。

回答

0

DataGridView.CurrentCell.RowIndex

DataGridView.CurrentRow.Index
將不考慮電流控制是組合框,文本框或只讀細胞給你當前行的索引。在你的邏輯中使用它。從財產的名稱顯而易見,它會給你當前的單元格或行對象。根據您的需要使用它。
我更喜歡使用CurrentRow屬性,因爲它直接給出當前行對象。而不是Cell屬性中的硬編碼列索引,我寧願使用ColumnObject.Index屬性。所以如果我們改變列順序,不需要改變代碼。
例如:

串源= dataGridView1.CurrentRow.Cells [colSourceName.Index] .Value.ToString();

(在這裏,我想SOURCE_NAME列名colSourceName)


編輯: 我用它在EditingControlShowing事件的DataGridView的。

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
{ 
    if (dataGridView1.CurrentCell.ColumnIndex == colSourceName.Index) 
    { 
     ((ComboBox)e.Control).SelectedIndexChanged -= cmb_SelectedIndexChanged; 
     ((ComboBox)e.Control).SelectedIndexChanged += cmb_SelectedIndexChanged; 
    } 
} 

首先它試圖分離事件,所以如果用戶第二次來到同一個組合框,它不會觸發它兩次。然後重新加入活動。
您有兩個組合框,所以您需要相應地添加條件。

+0

對不起。也許我的問題有點模糊。儘管您的建議確實有幫助,但我有興趣知道當組合框更改值時,可以使用哪些事件來獲取連續每列中的所有值。我希望你能給我你的輸入 – MRu

+0

我使用「EditingControlShowing」事件。請檢查編輯的答覆。 – par

+0

謝謝。這對我非常有幫助 – MRu

相關問題