Hiding of GridButtonColumn depending on row number

3 posts, 1 answers
  1. Dan Lehmann
    Dan Lehmann avatar
    103 posts
    Member since:
    May 2011

    Posted 14 Sep 2011 Link to this post

    Hi,
    I have a Grid that includes 2 GridButtonColumns. One of the buttons is MoveUp and the other is MoveDown. I need to hide the MoveUp button for the first row and hide the MoveDown button for the last row.

    Is there an example for hiding a GridButtonColumn in the codebehind?

    Thanks,
    Dan
  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 16 Sep 2011 Link to this post

    Hello Dan,

    The following code snippet shows the required functionality.
    C#:
    public partial class Grid_EditRight : System.Web.UI.Page
    {
        int rowCount, pageCount,lastpage;
        protected void grid_ItemDataBound(object sender, GridItemEventArgs e)
        {  
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                TableCell cell;
                item["Status"].Text = "NewText";
                int temp = item.OwnerTableView.CurrentPageIndex;
                if (temp == 0 && item.ItemIndex==0)
                {
                    cell = item["MoveUp"];
                    cell.Controls[0].Visible = false;
                }
                if (temp == lastpage-1 && item.ItemIndex==pageCount-1 )
                {
                    cell = item["MoveDown"];
                    cell.Controls[0].Visible = false;    
                }
            }
        }
        protected void grid_ItemEvent(object sender, GridItemEventArgs e)
        {
         if (e.EventInfo is GridInitializePagerItem)
            {
              rowCount = (e.EventInfo as GridInitializePagerItem).PagingManager.DataSourceCount;
            }
            pageCount = grid.MasterTableView.PageSize;
            lastpage = rowCount / pageCount;
        }
    }

    Thanks,
    shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Dan Lehmann
    Dan Lehmann avatar
    103 posts
    Member since:
    May 2011

    Posted 20 Sep 2011 Link to this post

    Thanks Shinu,
    I got it working based off your example. I wasn't using paging so the ItemEvent handler wasn't being called. Instead I just set _rowCount to the datasource.Count and included the following in my ItemDataBound:

    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim cell As TableCell
     
        If item.ItemIndex = 0 Then
            cell = item("MoveUp")
            cell.Controls(0).Visible = False
        End If
        If item.ItemIndex = _rowCount - 1 Then
            cell = item("MoveDown")
            cell.Controls(0).Visible = False
        End If
    End If

    Dan
Back to Top