中心datagrid组合框文本

时间:2013-03-15 07:32:21

标签: c# wpf wpfdatagrid

如何将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);
                    }
                }

1 个答案:

答案 0 :(得分:1)

WinForm DataGridView的解决方案:

在您创建ComboBox列的代码中添加下一行:

column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter