使用rowdatabound计算列数

时间:2014-05-13 12:16:56

标签: c# asp.net

我需要在事件rowdatabound中的gridview中找到总列数。是否有任何方法。 以下是我的几个代码:

   protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            LinkButton lnkView = new LinkButton();
            lnkView.ID = "lnkView";
            lnkView.Text = "View";
            lnkView.Click += ViewDetails; 
            e.Row.Cells[3].Controls.Add(lnkView);
        }

2 个答案:

答案 0 :(得分:3)

您可以将发件人转发给GridView并获取计数。

protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
{      
    if(!(sender is GridView))
        return;

    GridView gridView = (GridView) sender;
    var colCount = gridView.Columns.Count;

   //Your code
}

答案 1 :(得分:2)

不确定为什么要这样,但Cells是此行中单元格的数组,因此要获取列总数:

protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
    {            
        var colCount = e.Row.Cells.Count;
    }