簡體   English   中英

從DataGridView中的ComboBoxColumn更改選擇

[英]change selection from ComboBoxColumn in DataGridView

我有一個dataGridView與不同的列。 其中之一是帶有默認選擇(英語,德語,中文...)的ComboBoxColumn。 我以編程方式將新行添加到我的datagridview中。

dataGridView1.Rows.Add(sn, givenName, mail, department, ToDo);

第五列是我的ComboBoxColumn,當​​前寫為“ ToDo”。 我想說我應該選擇哪個comboBoxItems。 例如這樣:

dataGridView1.Rows.Add(sn, givenName, mail, department, 1);

現在應該在我的comboBox中選擇德語。 我將項目設置在Form1.designer.cs。

稍后,我想獲取為每個行選擇哪個Item的值。

您應該能夠說:

// Assuming your combo box column is named 'comboBoxColumn1'
dataGridView1.Rows.Add(sn, givenName, mail, department, comboBoxColumn1.Items[1]);

有關設置Items的示例,請參見此MSDN頁面

foreach (DataGridViewRow row in dataGridView1.Rows)
{
      DataGridViewComboBoxCell cell = row.Cells[0] as DataGridViewComboBoxCell;
}

現在您可以從單元變量訪問值。

編輯:當然,Cells []中的數字0不適合您的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM