如何将ComboBox列的文本置于DataGridView中?
使用不同类型的列动态更新DataGrid:
foreach (Fields field in fields)
{
if (field.group.Count > 1)
{
DataGridComboBoxColumn column = new DataGridComboBoxColumn();
column.Header = field.name;
column.ItemsSource = field.group;
column.SelectedValueBinding = new Binding(field.name)
{
};
dgwDataMain.Columns.Add(column);
}
else
{
DataGridTextColumn column = new DataGridTextColumn();
column.Header = field.name;
column.Binding = new Binding(field.name)
{
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
NotifyOnSourceUpdated = true,
NotifyOnTargetUpdated = true
};
dgwDataMain.Columns.Add(column);
}
}
答案 0 :(得分:1)
WinForm DataGridView
的解决方案:
在您创建ComboBox列的代码中添加下一行:
column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter