Maes
Maes

Reputation: 164

Count of number of columns using rowdatabound

I need to find count of total number of columns in a gridview inside the event rowdatabound .Is there any way for it. Below is my few code:

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

Upvotes: 1

Views: 5610

Answers (2)

Paddy
Paddy

Reputation: 33867

Not sure why you want this, but Cells is an array of the cells in this row, so to get the total number of columns:

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

Upvotes: 2

Dávid Kaya
Dávid Kaya

Reputation: 934

You can cast sender to GridView and get the count.

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

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

   //Your code
}

Upvotes: 3

Related Questions