我有一個DataGridView是從綁定到綁定源的DataTable填充的。我躲在通過DataTable的ColumnMapping您可以在手動添加的DataGridViewComboBox上排序DataGridView嗎?
dataTable.Columns[「ZoneId「].ColumnMapping = MappingType.Hidden;
我已經在DataGridView的DataBindingComplete
事件掛鉤,通過該行進行迭代,查找在數據綁定了zoneid列,然後設置組合框的行匹配了zoneid的方法列。
for (int i = 0; i < dataGridView.Rows.Count - 1; i++) { //Count-1 to ignore the editing row
// This is the DataGridView row, a subset of the backing data plus the combo and button cells
DataGridViewRow row = dataGridView.Rows[i];
// This is the full backing data for the row
DataRowView dataRow = (DataRowView)row.DataBoundItem;
if (dataRow != null) {
// Find cell with comboBox by name
DataGridViewComboBoxCell cell = DataGridViewComboBoxCell)row.Cells["ZoneIdComboBox"];
if (cell != null) {
Id = (string)dataRow["ZoneId"];
cell.Value = Id;
}
}
}
ComboBox的值等於存儲在表中的隱藏列值(ID)。文本部分是該值的人類可讀描述。這些由數據庫中的單獨表格定義。另外這兩個表有一個關係,所以我的主表中的ZoneId必須在我的ComboBox表中添加一個Id。
我可以單擊任何常規綁定列的標題並對錶格進行排序。我希望能夠單擊ComboBox列並對Text條目進行排序。