C# 如何为 DataGridView 设置标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/898265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to setup caption for DataGridView
提问by Ferdinand Chan
I'm using DataGridView in a WinForm app to show a table of data. Everything works fine except for the Caption Property of the DataColumn. I tried to set the Caption property but seems that DataGridView is using the Name of the DataColumn as the caption instead of the value of the Caption property ?
我在 WinForm 应用程序中使用 DataGridView 来显示数据表。除了 DataColumn 的 Caption 属性外,一切正常。我试图设置 Caption 属性,但似乎 DataGridView 使用 DataColumn 的名称作为标题而不是 Caption 属性的值?
Have google for this and seems that this caption property is deliberately disabled.
有谷歌这个,似乎这个标题属性被故意禁用。
My WinForm app is a localized one and I need to show caption in Chinese. Anyone know how can I do that?
我的 WinForm 应用程序是本地化的应用程序,我需要用中文显示标题。有谁知道我该怎么做?
Here is my code for setting up the data table
这是我设置数据表的代码
// Create a new DataTable.
DataTable table = new DataTable("Payments");
// Declare variables for DataColumn and DataRow objects.
DataColumn column;
DataRow row;
// Create new DataColumn, set DataType,
// ColumnName and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "id";
column.ReadOnly = true;
column.Unique = true;
column.Caption = LocalizedCaption.get("id") //LocalizedCaption is my library to retrieve the chinese caption
// Add the Column to the DataColumnCollection.
table.Columns.Add(column);
// Create three new DataRow objects and add them to the DataTable
for (int i = 0; i <= 2; i++)
{
row = table.NewRow();
row["id"] = i;
table.Rows.Add(row);
}
//assign the DataTable as the datasource for a DataGridView
dataGridView1.DataSource = table;
采纳答案by Nikias
This worked for me:
这对我有用:
private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
var dGrid = (sender as DataGrid);
if (dGrid == null) return ;
var view = dGrid.ItemsSource as DataView;
if (view == null) return;
var table = view.Table;
e.Column.Header = table.Columns[e.Column.Header as String].Caption;
}
回答by Robert Venables
You have a few options. Here's a quick fix that should work, just add this at the end of your block of code:
你有几个选择。这是一个应该有效的快速修复,只需在代码块的末尾添加:
//Copy column captions into DataGridView
for (int i = 0; i < table.Columns.Count; i++) {
if (dataGridView1.Columns.Count >= i) {
dataGridView1.Columns[i].HeaderText = table.Columns[i].Caption;
}
}
As you can see, this just copies over your existing column captions into the correct HeaderText property of each DataGridView column. This assumes that no previous columns exist in the DataGridView before you bind the DataTable.
如您所见,这只是将现有列标题复制到每个 DataGridView 列的正确 HeaderText 属性中。这假定在绑定 DataTable 之前 DataGridView 中不存在以前的列。