【问题标题】:Get the GridView Column Header Text - always returns blank获取 GridView 列标题文本 - 始终返回空白
【发布时间】:2013-01-10 14:59:25
【问题描述】:

我有一个 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
}

GridView 内容是在运行时根据用户查询生成的。标题可以点击排序。

如何循环 GridView 并列出列标题文本?

【问题讨论】:

  • 这段代码什么时候运行,即什么事件?
  • gridview1.Columns[ColumnIndex].HeaderText = "标题文本";
  • 代码在Page_Load的末尾运行
  • @krshekhar - 我不确定标题标题是什么?您的第二个想法无济于事,因为我试图不设置文本
  • 那是我的错。

标签: c# asp.net gridview


【解决方案1】:

你可以使用GridViewColumn.HeaderText

for (int i = 0; i < grdToDisplay.Columns.Count; i++)
{
    string header = grdToDisplay.Columns[i].HeaderText;
}

编辑

但这不会给我任何结果,因为列数是 0

然后你有AutoGenerateColumns=true 并且只计算以声明方式添加的列。因此,您已对GridView 进行数据绑定后使用此代码:

for (int i = 0; i < grdToDisplay.HeaderRow.Cells.Count; i++)
{
    string header = grdToDisplay.HeaderRow.Cells[i].Text;
}

【讨论】:

  • 从我上面的代码中可能不清楚,但这不会给我任何结果,因为列数是 0
  • 如果没有列,怎么会有Column HeaderText?
  • @jimmy: 那么你有AutoGenerateColumns=true 并且只会计算以声明方式添加的列。
  • 我刚刚检查过,AutoGenerateColumns 是真的。我尝试将其设置为 false,但没有显示任何内容。 - 我没有看到你的编辑,让我测试一下
  • 在 grdToDisplay.DataBind() 之后;我现在有了你的代码,但 grdToDisplay.HeaderRow.Cells[i].Text 仍然返回空白
【解决方案2】:

由于 GridView 的 header 是可排序的,因此您可以从 Linkbutton 获取 header 的文本。试试这个: 将以下代码放入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;

                    }

                }

            }

【讨论】:

  • 喜欢它..将其压缩为 ((LinkBut​​ton)e.Row.Cells[3].Controls[0]).Text
【解决方案3】:

我根据我在这里和其他线程中阅读的内容创建了自己的解决方案.. 就这样:

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;
                        }
                    }

                }
            }
        }

该方法直接隐藏列的标题和单元格,其中标题名称(或列名称)是传递给我的方法的字符串参数(“标题”参数)。然后从我的 gridview 的“DataBound”事件中调用“HideColumnByName”方法。简单的。希望这可以帮助 !它确实对我有用! :)

【讨论】:

  • 这与问的问题无关
【解决方案4】:

我最近在进行一些最近的编码时遇到了这个问题,每当我尝试获取标题文本时,即使在数据绑定事件上它也总是返回空白。当我想尝试根据列自己的控制方案转换标题时,我才想到解决方案。

事实证明,使 gridview 可排序会将标题文本转换为其他内容,从而无法简单地从标题中获取信息而不进行转换。我自己对这个问题的解决方案是在我要求文本时简单地让它不可排序,但假设这是不可能的,我确实遇到了获取文本的解决方案,即使它可以在另一个线程中排序:

ASP.NET GridView header row text empty when AllowSorting enabled

【讨论】:

    【解决方案5】:


    如果模板列基于数据源,则标题文本返回文件名

    • GridView.Columns(i).HeaderText

    但如果您需要先获取列的标题文本,请使用该行

    • GridView.HeaderRow.Cells(i).Text

    此链接可能对您有所帮助 Get Header Text of Gridview Cell

    【讨论】:

      【解决方案6】:
      grdExcel.HeaderRow.Cells[0].Text.ToString();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多