Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about DataGrid Template Column 1

Status
Not open for further replies.

JGresko

Programmer
Apr 24, 2002
86
US
I have a datagrid where one column contains two image buttons allowing people to move the row up or down in the grid. My question is, is there a way to make the up arrow not visible if the row is at the top... same thing with the down arrow if the row is at the bottom?

It currently looks like this:
Code:
<asp:TemplateColumn HeaderText=&quot;Move&quot;>
     <HEADERSTYLE WRAP=&quot;False&quot; HORIZONTALALIGN=&quot;Center&quot; 
                  VERTICALALIGN=&quot;Middle&quot;></HeaderStyle>

     <ITEMSTYLE WRAP=&quot;False&quot; HORIZONTALALIGN=&quot;Center&quot;
                VERTICALALIGN=&quot;Middle&quot;></ItemStyle>

     <ITEMTEMPLATE>
	  <ASP:IMAGEBUTTON id=btnUp runat=&quot;server&quot; 
               BorderWidth=&quot;0px&quot; CausesValidation=&quot;true&quot; 
               ImageUrl=&quot;images/arrow1b1.jpg&quot; 
               CommandName=&quot;btnUp&quot;></ASP:IMAGEBUTTON>
            
          <ASP:IMAGEBUTTON id=bntDown runat=&quot;server&quot; 
               BorderWidth=&quot;0px&quot; CausesValidation=&quot;true&quot; 
               ImageUrl=&quot;images/arrow2b2.jpg&quot;  
               CommandName=&quot;btnDown&quot;></ASP:IMAGEBUTTON>
     </ItemTemplate>

     <FOOTERSTYLE WRAP=&quot;False&quot;></FooterStyle>
</asp:TemplateColumn>
 
Let's say that the column in question is the first column in the grid (the 0th position). You could add an onItemDataBound event handler and selectively show/hide the buttons like so:


~~~~~
'class (global) declaration
dim topHidden as boolean
~~~~~

protected sub handleItemDataBound(o as object, e as DataGridItemEventArgs) handles myDataGrid.itemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
if not topHidden then
e.item.cells(0).findControl(&quot;btnUp&quot;).visible = false
topHidden = true
end if
if e.Item.DataSetIndex = DataGrid1.Items.Count then
e.item.cells(0).findControl(&quot;btnDown&quot;).visible = false
end if
End If
end sub

These routines will certainly take some tweaking, as you may have a footer or some other row that might make the dataSetIndex of the e.item not equal to the items.count property of the datagrid when it's on your last row, and you might have some other things going on in your grid that I don't know about... but that is the general gist of what you have to do, and the base objects that I think you'll need to get the job done.

And sorry for the formatting, too. They're long statements.

hth! :)
paul
penny1.gif
penny1.gif
 
Thanks Again Paul!

That was the right direction. I ended up using a variation of your code and putting it in a Grid Binding proceedure (see below). It's working great, Thanks, Judy

Code:
Sub BindGrid(ByVal dg As DataGrid, ByVal dv As DataView)
   dg.DataSource = dv
   dg.DataBind()
   If dg Is dgColumns Then
      If dg.Items.Count() > 0 Then
         dgColumns.Items(0).Cells(2).FindControl(&quot;btnUp&quot;).Visible = False
         dgColumns.Items(dgColumns.Items.Count() - 1).Cells(2).FindControl(&quot;btnDown&quot;).Visible = False
      End If
   End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top