DataTable DataColumn ColumnName不显示 [英] DataTable DataColumn ColumnName not displaying
问题描述
我是VSTO的新手,遇到了一个似乎无法解决的问题。我试图在Excel 2013中显示一个简单的表格,并且所有内容都在填充,除了列显示名称出来是 Column1,Column2,Column3。
I am new to VSTO and am encountering an issue I can't seem to figure out. I am trying to display a simple table in Excel 2013 and everything populates, except the column display names come out to be "Column1, Column2, Column3".
这是我的代码:
var worksheet = Globals.Sheet1;
worksheet.Cells.Clear();
var table = new DataTable("Users");
// Set Columns
var columns = new List<DataColumn>
{
new DataColumn("Staged") { ColumnName = "Staged", Caption = "Staged"},
new DataColumn("FirstName") { ColumnName = "First Name", Caption = "First Name" },
new DataColumn("LastName") { ColumnName = "Last Name", Caption = "Last Name"}
};
foreach (var column in columns)
{
table.Columns.Add(column);
}
var lastFilledRow = 1;
// Populate data
for (var i = 1; i < 11; i++)
{
var row = table.NewRow();
row[table.Columns[0]] = "";
row[table.Columns[1]] = "Joesph " + i;
row[table.Columns[2]] = "Bellow " + i;
table.Rows.Add(row);
lastFilledRow++;
}
var lastCell = "C" + lastFilledRow;
worksheet.Controls.Remove("userList");
var list = worksheet.Controls.AddListObject(worksheet.Range["A1", lastCell], "userList");
list.SetDataBinding(table);
在调试模式下单步执行代码,从头到尾我看不到任何设置为 Column1,但它们设置为提供的值。我知道这些是ColumnName的默认值。
Stepping through the code in debug mode, all the way through the code I cannot see any attributes set to "Column1", but they are set to the values provided. I know these are the default values for ColumnName.
我的问题是,为什么在我明确设置excel时,为什么在excel中将ColumnNames打印为Column1,Column2,Column3?列名的值?
My question is, Why are the ColumnNames printing out in excel as Column1, Column2, Column3 when I'm explictly setting the values for ColumnName?
推荐答案
您需要显式命名列。在 var list = ...
之后,添加以下内容:
You need to name the columns explicitly. After var list = ...
add the following:
list.ListColumns[1].Name = "Staged";
list.ListColumns[2].Name = "First Name";
list.ListColumns[3].Name = "Last Name";
这篇关于DataTable DataColumn ColumnName不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!