我與在windowsforms 在DataGridView的組合框delaeaing假設我有一個datagridview的2個combboxes所以我的問題是
如何根據本datagridview的從另一個comboboxcolum選擇的值填充在datagridview的comboboxcolumn2組合框在datagridview的
回答
嗯,我想我遲到了,但你應該做的是將價值改變的事件處理程序附加到你的列。處理程序應該在您選擇新值時觸發,然後使用傳遞給該函數的參數來確定哪一行被更改,並更改該行中的第二個組合框。我會舉一個例子,但我幾乎肯定你不會回過頭來看看這個答案。
編輯: 沒想到你真的會爲此而復出,但這裏有一種方法來實現動態ComboBoxes。我不能說我完全爲這個答案感到自豪,因爲它不適用於數據綁定(你將不得不自己添加每一行並設置適當的初始值),但至少這個解決方案可以指導你朝着正確的方向或者一個新的。
public partial class DualComboBoxGridViewForm : Form
{
private Dictionary<Country, List<City>> locations;
public DualComboBoxGridViewForm()
{
InitializeComponent();
InitializeLocations();
dataGridView1.Rows.Add(new DataGridViewRow());
}
private void InitializeLocations()
{
// Loading these from a database would be highly recommended, I
// just did it like this with a dictionary to it would be easier
// to show.
locations = new Dictionary<Country, List<City>>();
List<City> americanCities = new List<City>();
americanCities.Add(new City { ID = 0, Name = "Please Select A City" });
americanCities.Add(new City { ID = 1, Name = "Boston" });
americanCities.Add(new City { ID = 2, Name = "New York" });
List<City> japaneseCities = new List<City>();
japaneseCities.Add(new City { ID = 0, Name = "Please Select A City" });
japaneseCities.Add(new City { ID = 1, Name = "Tokyo" });
japaneseCities.Add(new City { ID = 2, Name = "Kyoto" });
locations.Add(new Country { ID = 0, Name = "Please Select A Country" }, new List<City>());
locations.Add(new Country { ID = 1, Name = "USA" }, americanCities);
locations.Add(new Country { ID = 2, Name = "Japan" }, japaneseCities);
}
private void InitializeDataGridView()
{
dataGridView1.AutoGenerateColumns = false;
dataGridView1.EditingControlShowing += dataGridView1_EditingControlShowing;
// Create columns
DataGridViewTextBoxColumn eventNameColumn = new DataGridViewTextBoxColumn();
eventNameColumn.HeaderText = "Event";
DataGridViewComboBoxColumn countryComboBox = new DataGridViewComboBoxColumn();
countryComboBox.Name = "Country";
countryComboBox.HeaderText = "Country";
countryComboBox.ValueMember = "ID";
countryComboBox.DisplayMember = "Name";
foreach (Country country in locations.Keys)
{
countryComboBox.Items.Add(country);
}
DataGridViewComboBoxColumn cityComboBox = new DataGridViewComboBoxColumn();
cityComboBox.Name = "City";
cityComboBox.HeaderText = "City";
cityComboBox.ValueMember = "ID";
cityComboBox.DisplayMember = "Name";
dataGridView1.Columns.Add(eventNameColumn);
dataGridView1.Columns.Add(countryComboBox);
dataGridView1.Columns.Add(cityComboBox);
}
// Triggers when a column enters edit mode (new value not yet assigned).
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs args)
{
// we only want to change the city box if a country value is changed
if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns["Country"].Index)
{
ComboBox countryBox = args.Control as ComboBox;
countryBox.SelectedIndexChanged += countryComboBox_SelectedIndexChanged;
}
}
private void countryComboBox_SelectedIndexChanged(object sender, EventArgs args)
{
ComboBox box = sender as ComboBox;
DataGridViewComboBoxCell cityCell = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[dataGridView1.Columns["City"].Index] as
DataGridViewComboBoxCell;
cityCell.Items.Clear();
foreach (City city in locations[box.SelectedItem as Country])
{
cityCell.Items.Add(city);
}
if (cityCell.Items.Count > 0)
{
cityCell.Value = cityCell.Items[0];
}
// Remove event handler to prevent memory leak
box.SelectedIndexChanged -= countryComboBox_SelectedIndexChanged;
}
}
public class Country
{
public int ID { get; set; }
public string Name { get; set; }
}
public class City
{
public int ID { get; set; }
public string Name { get; set; }
}
@Denjamin 我復出,我等着你的榜樣 – 2012-10-15 11:31:20
@mohammedsameeh我爲沒有找到一種方法,使數據綁定的工作表示歉意,但它是相當棘手,因爲您需要在數據綁定時動態分配城市組合框。您可以使用虛擬模式執行此操作,但只有在列未被綁定時才能觸發事件。一個解決方法是創建綁定到實際數據的隱藏列,並使一些可見的未綁定列顯示下拉列表(只需使用像EditingControlShowing這樣的事件來更新隱藏的數據綁定列。 – 2012-10-15 17:54:43
- 1. DataGridView組合框
- 2. dataGridView與組合框
- 3. datagridview中的c#組合框
- 4. Datagridview - 多列組合框
- 5. DataGridView組合框和主鍵
- 6. 組合框綁定DataGridView
- 7. DataGridView組合框列綁定
- 8. DataGridView中的動態組合框
- 9. datagridview中的可移動組合框
- 10. C#的DataGridView十進制組合框柱
- 11. DataGridView的組合框格室C#
- 12. DataGridView的組合框擴大發行
- 13. DataGridView的數據集和組合框
- 14. 關於vb.net中datagridview的組合框列
- 15. 在綁定到集合的datagridview中設置組合框
- 16. 如何把在datagridview的組合框編程迭代集合
- 17. datagridview單元格和組合框
- 18. C#DataGridView組合框綁定問題
- 19. C#Datagridview組合框選擇價值棒
- 20. Datagridview和兩個組合框(相關)
- 21. 使用多個組合框過濾datagridview
- 22. 卡住datagridview和組合框列
- 23. Datagridview虛擬模型組合框
- 24. 從組合框1中選擇的值填充組合框2
- 25. 在datagridview中組合兩列?
- 26. 問題綁定在datagridview內的組合框中的值
- 27. 在datagridview單元格中的組合框上應用事件。 (DatagridViewColoumnComboBox)
- 28. 檢索,在組合框中選擇datagridview的
- 29. 如何手動在datagridview中設置組合框中的項目?
- 30. 在datagridview組合框中顯示來自sql server的數據
你的意思是說,你有兩個組合框的每一行的組合框? – yonan2236 2010-10-16 03:08:26
是的,我的意思是這 – 2010-10-16 04:45:18
請回答,因爲這個問題是當務之急謝謝^ _^ – 2010-10-16 17:29:12