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

Datagrid - displaying columns based on code

Status
Not open for further replies.

MikeMCSD

MIS
Jul 12, 2002
107
US
Is it possible to use code before displaying columns on
the datagrid to change a column based on a condition?

Here is what I want to do: I Have a field "Engraving" in
the database that if True, I want to display "Yes" and an
Add button next to it. If false, just "No".
I use this to get the data from the database:

Datalist.DataSource = Catalog.GetProducts(CategoryId)

******************
Can I put code here before displaying the datagrid????
I want to check the "Engraving" field here and take
action based on T or F.
******************

Datalist.DataBind()


Thanks
 
Mike: You might be able to use a modification of the following approach, it involves a call to a function in th code behing which is referenced during the binding event of the DataGrid (there is a specific name for these functions but it escapes me at the moment). For this example I am calling either a red or a green image of a flag, depending on status.

Code behind Function
Code:
Protected Function WhichImage(Active As Object) As String
  If Active = 1 Then
     Return "Images/GreenFlag.gif"
  Else
     Return "Images/RedFlag.gif"
  End If
End Function

The images are maintained within a Template Column in the DataGrid and appears as:

Code:
<asp:TemplateColumn HeaderText="Act?">
 <HeaderStyle horizontalalign="Center" verticalalign="Middle"/>
 <ItemStyle horizontalalign="Center"/>
   <ItemTemplate>
    <asp:Image id="imgAct" Runat="Server" ImageUrl=<%# WhichImage(Container.DataItem("Active")) %>/>
   </ItemTemplate>
</asp:TemplateColumn>

The call to the function is made within the Template Column during binding of the DataGrid. Perhaps you can modify this approach to accomplish what you need. Otherwise you might have to use the ItemDataBind Event of the Grid.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top