简体   繁体   English

如何更改WPF Datagrid上的默认列标题?

[英]How to change default Column Header on WPF Datagrid?

I have a WPF Datagrid whose ItemsSource is provided dynamically at run-time from a DB table. 我有一个WPF Datagrid,它的ItemsSource在运行时从数据库表中动态提供。 For simplicity (?) I'm dumping the table contents into a 2-D array (the ItemsSource previously mentioned) because I don't know in advance how many columns any particular table may have. 为了简单起见(?),我将表内容转储到2-D数组(前面提到的ItemsSource)中,因为我事先不知道任何特定表可能有多少列。 Hence, AutoGenerate is set to True. 因此,自动生成设置为True。

No problems with this - works fine; 没问题-运作良好; BUT the default behaviour of the grid is to number the columns from zero and display this number in the column header - I need them to number from one... I've messed around with DisplayIndex, etc. but to no avail. 但是网格的默认行为是从零开始对列进行编号,并在列标题中显示该数字-我需要它们从一开始进行编号...我搞砸了DisplayIndex等,但无济于事。 Anyone got any ideas on how I can alter/edit the default naming convention? 任何人都对如何更改/编辑默认命名约定有任何想法?

Just had a thought - would anyone suggest I catch the AutoGeneratedColumns event and manually count the columns then rename in a loop? 只是有一个想法-有人会建议我抓住AutoGeneratedColumns事件并手动计算列,然后在循环中重命名吗? But of course the display to the user as the thing re-writes itself might be a bit strange... :) 但是,当事物重新编写时,显示给用户的显示当然可能有点奇怪... :)

Been a looong day - starting to babble! 充实的一天-开始day不休!

I'm using VS2012 Express, the program is in C#. 我正在使用VS2012 Express,该程序在C#中。

I don't understand. 我不明白 It isn't possible to set 2-D array as ItemsSource. 不可能将二维数组设置为ItemsSource。

If you use collection of entites then column header should be equal to property name. 如果使用实体集合,则列标题应等于属性名称。
If you'd like to change it, then there is an attribute called DisplayName : 如果您想更改它,则有一个名为DisplayName的属性:
http://msdn.microsoft.com/en-us/library/system.componentmodel.displaynameattribute%28VS.90%29.aspx http://msdn.microsoft.com/zh-cn/library/system.componentmodel.displaynameattribute%28VS.90%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WPF:如何冻结数据网格中的列标题 - WPF: How to freeze column header in datagrid WPF:当鼠标位于列标题上时,如何更改datagrid列的属性? - WPF: How to change properties of a datagrid column when mouse is over the header of the column? 如何通过WPF DataGrid中的DataGrid Header CheckBox选择列的所有CheckBox - How to Select All CheckBox of a Column by DataGrid Header CheckBox in WPF DataGrid 如何选择WPF DataGrid中DataGrid Header CheckBox上列的所有CheckBox - How to Select All CheckBox of a Column on DataGrid Header CheckBox in WPF DataGrid 在运行时使用自动生成的列更改WPF数据网格中列标题的颜色 - Change colour of column header in WPF datagrid with autogenerated columns at runtime C#WPF DataGrid通过标题位置更改列索引 - C# WPF DataGrid change Column Index by Position of Header 如何在WPF中更改DataGrid中的列宽 - How to change the width of column in datagrid in wpf 在绑定到DataTable的WPF DataGrid上,如何通用地更改“列标题”模板? - On a WPF DataGrid bound to a DataTable, how can I generically change the Column Header template? 使用 WPF 数据网格时如何更改列标题的背景颜色 - How to change column header's background color when using WPF datagrid 如何在后面的代码中更改DataGrid列的标题? - How to change the header of a DataGrid column in code behind?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM