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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Optionally Display Value in Datagrid 1

Status
Not open for further replies.

byrne1

Programmer
Aug 7, 2001
415
US
I want to display a hyperlink on a datagrid row if a certain value in that row meets a criteria. For the sake of discussion, let's say I have a datagrid w/ three template colums. The datagrid is bound to a dataset. The first column contains a unique ID field, the second column contains a label for a value and the third column is where I want to optionally display my hyperlink.

If the value in the second column is, let's say, "Foreign" then I want to display a hyperlink in the third column that will redirect the user to a page and pass to that page the value in the first column.

For example, if the first column contains "1234", the second column contains "Foreign", then I want a hyperlink in the third column to be something like "map.aspx?id=1234". Below are how I have my columns set up so far. I would be very grateful for any help you may provide.


<asp:datagrid id=&quot;DataGrid1&quot; runat=&quot;server&quot; Font-Size=&quot;10pt&quot; AlternatingItemStyle-BackColor=&quot;#ffffcc&quot; autogeneratecolumns=&quot;False&quot; DataKeyField=&quot;IDNbr&quot; OnEditCommand=&quot;DataGrid_Edit&quot; OnCancelCommand=&quot;DataGrid_Cancel&quot; OnUpdateCommand=&quot;DataGrid_Update&quot; AllowPaging=&quot;True&quot; PageSize=&quot;15&quot;>
<AlternatingItemStyle BackColor=&quot;#FFFFCC&quot;></AlternatingItemStyle>
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField=&quot;IDNbr&quot; DataNavigateUrlFormatString=&quot;Roundhouse.aspx?IDNbr={0}&quot; DataTextField=&quot;IDNbr&quot; HeaderText=&quot;ID #&quot; NavigateUrl=&quot;Roundhouse.aspx&quot;>
<HeaderStyle Font-Bold=&quot;True&quot; HorizontalAlign=&quot;Center&quot; ForeColor=&quot;Yellow&quot; BackColor=&quot;DarkBlue&quot;></HeaderStyle>
</asp:HyperLinkColumn>
<asp:TemplateColumn>
<HeaderStyle Font-Bold=&quot;True&quot; ForeColor=&quot;Yellow&quot; BackColor=&quot;DarkBlue&quot;></HeaderStyle>
<HeaderTemplate>
Debt Type
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID=&quot;lblFgnDom&quot; Runat=&quot;server&quot; Text='<%# DataBinder.Eval(Container, &quot;DataItem.FgnDom&quot;) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderStyle ForeColor=&quot;Yellow&quot; BackColor=&quot;DarkBlue&quot;></HeaderStyle>
<ItemTemplate>
<asp:HyperLink ID=&quot;AmortLink&quot; Runat=&quot;server&quot;>
<asp:Label ID=&quot;lblAmort&quot; Runat=&quot;server&quot;></asp:Label>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
 
Probably the best way to do this is to use the &quot;ItemDataBound&quot; event of the DataGrid. This event is called for each row of data that is put into the datagrid. Then, for each row, you can check the value in second column and determine whether or not the button in the third column should be visible.

The following code is an example of this. It checks to see if column 2's value is &quot;Foreign&quot;. If so, it sets the button in column 3 to visible. Else, it makes it invisible.

Private Sub myDataGrid_ItemDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles myDataGrid.ItemDataBound
If Not e.Item.ItemType = Web.UI.WebControls.ListItemType.Header And _
Not e.Item.ItemType = Web.UI.WebControls.ListItemType.Footer Then
Dim Column2 As String
Column2 = e.Item.Cells(1).Text
If Column2 = &quot;Foreign&quot; Then
e.Item.Cells(2).Controls(0).Visible = True
Else
e.Item.Cells(2).Controls(0).Visible = False
End If
End If
End Sub

HTH! Kevin B.
.Net Programmer [thumbsup]
 
That worked perfectly!!! Thank you very much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top