DataColumn.Caption属性在DataTable绑定到DataGridView上时,并未像想象中那样做为HeaderText来显示。
微软官网貌似说是个小bug,从2.0至今未修正。
DataColumn.Caption属性应用到DataGridView.HeaderText的方法有两种,网上整理:
for (int i = 0; i < table.Columns.Count; i++) {
if (dataGridView1.Columns.Count >= i) {
dataGridView1.Columns[i].HeaderText = table.Columns[i].Caption;
}
}
if (dataGridView1.Columns.Count >= i) {
dataGridView1.Columns[i].HeaderText = table.Columns[i].Caption;
}
}
或:
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.HeaderText = table.Columns[e.Column.Header as String].Caption;
}
{
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.HeaderText = table.Columns[e.Column.Header as String].Caption;
}