0

我在Visual Studio 2010中使用C#。我有一個DataGridViewComboBoxColumn綁定到一個DataSource。我希望從組合框中進行選擇時,整行將更改顏色。我不知道如何做到這一點,因爲組合框沒有自己的獨立名稱,它們都屬於cbColumn1。我在這裏使用這篇文章,但不知道如何使代碼工作。 How do I immediately change the row color when the selected index of a DataGridViewComboBox changes?基於DataGridViewComboBoxColumn選擇的顏色行

任何幫助,將不勝感激

+1

我在我的Mac,但我認爲你需要定義樣式您DataGridRow並把觸發有數據觸發,這將在viewmodel.selected項目和DataGrid行選擇工作狀態,所以該行被選中,組合框選擇值改變了背景變化 – whoisthis

回答

0

像這樣的東西應該工作...你可能要調整了一點,因爲我測試了它在我自己的程序,可設置不同的相當。

DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn(); 
column.Items.Add("0"); 
column.Items.Add("1"); 
column.Items.Add("2"); 
dataGridView1.Columns.Add(column); 



private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
    { 
     if (dataGridView1.CurrentCell.ColumnIndex == 1) 
     { 
      ComboBox comboBox = e.Control as ComboBox; 
      comboBox.DropDownStyle = ComboBoxStyle.DropDown; 
      comboBox.SelectedIndexChanged += comboBox_SelectedIndexChanged; 
     } 
    } 


void comboBox_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     int selectedIndex = ((ComboBox)sender).SelectedIndex; 
     DataGridViewCellStyle style = new DataGridViewCellStyle(); 
     if (selectedIndex == 0) 
     { 
      style.BackColor = Color.AliceBlue; 
     } 
     if (selectedIndex == 1) 
     { 
      style.BackColor = Color.Beige; 
     } 
     if (selectedIndex == 2) 
     { 
      style.BackColor = Color.Crimson; 
     } 
     dataGridView1.CurrentCell.OwningRow.DefaultCellStyle = style; 
     dataGridView1.Refresh(); 
    }