加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 获取GridView列标题文本 – 始终返回空白

发布时间:2020-12-15 08:41:20 所属栏目:百科 来源:网络整理
导读:我有一个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 = grdToDispl
我有一个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并列出列标题文本?

解决方法

你可以使用 GridViewColumn.HeaderText
for (int i = 0; i < grdToDisplay.Columns.Count; i++)
{
    string header = grdToDisplay.Columns[i].HeaderText;
}

编辑:

but this would give me no results as the columns count is 0

然后你有AutoGenerateColumns = true并且只计算声明性添加的列.因此,在对GridView进行数据绑定后使用此代码:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读