This is a migrated thread and some comments may be shown as answers.

Hiding of GridButtonColumn depending on row number

2 Answers 75 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dan Lehmann
Top achievements
Rank 1
Dan Lehmann asked on 14 Sep 2011, 08:54 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 16 Sep 2011, 03:19 PM
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.
0
Dan Lehmann
Top achievements
Rank 1
answered on 20 Sep 2011, 11:12 PM
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
Tags
Grid
Asked by
Dan Lehmann
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Dan Lehmann
Top achievements
Rank 1
Share this question
or