使用列表填充并使用“自动生成”字段时,无法更改列名
本文关键字:自动生成 字段 填充 列表 | 更新日期: 2023-09-27 18:20:40
默认列名为item1、item2和item3,我无法更改它们,我在这里看到了类似的问题,但似乎没有一个解决方案有效。
if (depositTotal != cartTotal & depositTotal != 0.0)
{
list.Add(Tuple.Create(amazonOrderID, depositTotal, cartTotal));
}
dataGridTotals.DataSource = list;
dataGridTotals.DataBind();
我试过在绑定gridview数据前后使用它,但它不起作用:
dataGridTotals.Columns[0].HeaderText = "New Header for Column";
有人有其他想法吗?
自动生成的列不会填充列列表。您需要循环浏览第一行的单元格控件并手动更改文本。
将此方法用于网格的RowDataBound
事件。它将捕获标题行并重新分配文本值。此代码假定您知道接收列的顺序。如果没有,你可以阅读每一行的文本并采取相应的行动。。。
private void gvMyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
// Apply to header only.
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "Column 1 Text";
e.Row.Cells[1].Text = "Column 2 Text";
e.Row.Cells[2].Text = "Column 3 Text";
}
}
上面的代码假设您知道接收列的顺序。如果没有,你可以阅读每一行的文本并采取相应的行动:
private void gvMyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
// Apply to header only.
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (Cell cell in e.Row.Cells)
{
if (cell.Text == "FName")
{
cell.Text = "First Name";
}
else if (cell.Text == "LName")
{
cell.Text = "Last Name";
}
else if (cell.Text == "Etc")
{
cell.Text = "Et cetera";
}
}
}
}
在后一种情况下,您可能希望设置一个SQL字段名称和友好的英文名称的字典来阅读,这样您就不会有50个if
语句,但您已经明白了。