代码之家  ›  专栏  ›  技术社区  ›  Ian manuel aldana

GridView即使在DataBind()之后也不存在列

  •  2
  • Ian manuel aldana  · 技术社区  · 17 年前

    我有一个已经填充了数据的DataView(已验证此为真)。

    绑定后,我检查了GridView的列计数(grid.Columns.count),结果显示为0。但它显示了15列的正确输出。

    此外,使用列的索引访问列将引发异常。

    我实际上需要在列标题中添加一个“字形”(向上/向下箭头),以显示正在排序的列及其方向。下面的代码是我正在使用的。问题是,列。计数始终为零。

        for (int i = 0; i < dgData.Columns.Count; i++)
        {
            string colExpr = dgData.Columns[i].SortExpression;
            if (colExpr != "" && colExpr == dgData.SortExpression)
                item.Cells[i].Controls.Add(glyph);
        }
    
    1 回复  |  直到 17 年前
        1
  •  2
  •   Eoin Campbell    17 年前

    编辑

    试试这个,它有点笨拙,因为它依赖于根据GridView SortExpression测试链接按钮文本。Gridview ID是“测试”

        protected void test_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                foreach (TableCell tc in e.Row.Cells)
                {
                    if(test.SortExpression.Contains( (tc.Controls[0] as LinkButton).Text ))
                        tc.Controls.Add( glyph )
                }
            }
        }
    

    如果您自动生成列,我认为不会设置列集合。..

    你可以查一下 Row.Cells.Count

    foreach(Row r in GridView.Rows)
    {
        if(r.RowType == HeaderRow)
        {
             r.Cells[0].Controls[0]; //Link Control is here.
        }
    }   
    
    推荐文章