C# 获取 GridView 列标题文本 - 始终返回空白
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14260753/
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
Get the GridView Column Header Text - always returns blank
提问by jimmy
I have a GridView that I would like to get the Text of the columns headers from.
我有一个 GridView,我想从中获取列标题的文本。
for (int j = 0; j < grdToDisplay.HeaderRow.Cells.Count; j++)
{
string s = grdToDisplay.HeaderRow.Cells[j].Text.ToString(); //Returns ""
s = grdToDisplay.HeaderRow.Cells[j].Text; //Returns ""
s = grdToDisplay.Rows[0].Cells[j].Text; //Returns text from first row of results not the header
// s = grdToDisplay.Columns[j].HeaderText.ToString(); // does not work as column count is 0
}
The GridView contents are generated at runtime based on a user query. The header is click-able to sort.
GridView 内容是在运行时根据用户查询生成的。标题可点击排序。
How can I loop the GridView and list the column header text?
如何循环 GridView 并列出列标题文本?
回答by Tim Schmelter
You can use GridViewColumn.HeaderText
您可以使用 GridViewColumn.HeaderText
for (int i = 0; i < grdToDisplay.Columns.Count; i++)
{
string header = grdToDisplay.Columns[i].HeaderText;
}
Edit:
编辑:
but this would give me no results as the columns count is 0
但这不会给我任何结果,因为列数为 0
Then you have AutoGenerateColumns=true
and only declaratively added columns are counted. So use this code afteryou have databound the GridView
:
然后你有AutoGenerateColumns=true
并且只计算声明性添加的列。因此,在对以下内容进行数据绑定后使用此代码GridView
:
for (int i = 0; i < grdToDisplay.HeaderRow.Cells.Count; i++)
{
string header = grdToDisplay.HeaderRow.Cells[i].Text;
}
回答by Parsa
Since header of GridView is sortable, you can get the text of the header from Linkbutton. try this one: put the following code in the DataBind of the gridView:
由于 GridView 的标题是可排序的,因此您可以从 Linkbutton 获取标题的文本。试试这个:将以下代码放在 gridView 的 DataBind 中:
for (int i = 0; i < gridView.HeaderRow.Cells.Count; i++)
{
if (gridView.HeaderRow.Cells[i].HasControls())
{
//when gridview is sortable, type of header is LinkButton
// Linkbutton is in index 0 of the control
if (gridView.HeaderRow.Cells[i].Controls[0] is LinkButton)
{
LinkButton headerControl = gridView.HeaderRow.Cells[i].Controls[0] as LinkButton;
string headerName = headerControl.Text;
}
}
}
回答by FaKu Soler
I created my own solution based on things i′ve read here and in other threads .. here it goes:
我根据我在此处和其他线程中阅读的内容创建了自己的解决方案......这里是:
public void HideColumnByName(GridView grid, string header)
{
if (grid.HeaderRow.HasControls()==true)
{
for (int i = 0; i < grid.HeaderRow.Cells.Count; i++)
{
if (grid.HeaderRow.Cells[i].Text == header)
{
foreach (GridViewRow row in grid.Rows)
{
row.Cells[i].Visible = false;
grid.HeaderRow.Cells[i].Visible = false;
}
}
}
}
}
that method straight up hides both Headers and Cells of the column which header name (or column name) is the string parameter passed to my method ('header' param). "HideColumnByName" method is then called from the "DataBound" event of my gridview. Simple. Hope this helps ! It sure did work for me ! :)
该方法直接隐藏了列的标题和单元格,其中标题名称(或列名称)是传递给我的方法的字符串参数('header' 参数)。然后从我的 gridview 的“DataBound”事件中调用“HideColumnByName”方法。简单的。希望这可以帮助 !它确实对我有用!:)
回答by ShadowX
I recently came across this very problem when doing some recent coding, anytime I tried getting the header text it would always return blank even on data bound events. I only came to the solution when I thought to try converting the header based on the columns own control scheme.
我最近在做一些最近的编码时遇到了这个问题,每当我尝试获取标题文本时,即使在数据绑定事件中它也总是返回空白。当我想尝试根据列自己的控制方案转换标题时,我才想到解决方案。
It turns out, making a gridview sortable turns the header text into something else, making it impossible to simply get the information from the header without converting it. My own solution to the problem was to simply have it not be sortable when I asked for the text, but assuming that isn't possible I did come across the solution to getting the text even while it is sortable in another thread:
事实证明,使 gridview 可排序会将标题文本转换为其他内容,从而无法简单地从标题中获取信息而不进行转换。我自己解决这个问题的方法是在我要求文本时简单地让它不可排序,但假设这是不可能的,我确实遇到了获取文本的解决方案,即使它在另一个线程中是可排序的:
ASP.NET GridView header row text empty when AllowSorting enabled
回答by Mostafa Anssary
the header text return the filed name if template column base on data-source
- GridView.Columns(i).HeaderText
- GridView.Columns(i).HeaderText
but if what you need to get header text for column first use that line
但是,如果您需要先获取列的标题文本,请使用该行
- GridView.HeaderRow.Cells(i).Text
- GridView.HeaderRow.Cells(i).Text
this link may help you Get Header Text of Gridview Cell
此链接可以帮助您 获取 Gridview 单元格的标题文本