Display DataGrid Column as Hyperlink Column depending on column value

1.9k views Asked by At

I have the below DataGrid which works with no problem

<asp:DataGrid ID="fileBrowserGrid" runat="server" Width="100%" PageSize="14" AllowPaging="True"
        CellPadding="1" GridLines="None" BorderColor="#636E92" BorderWidth="0px" AutoGenerateColumns="False"
        OnPageIndexChanged="fileBrowserGrid_PageIndexChanged">
        <AlternatingItemStyle CssClass="mainbodytextalt"></AlternatingItemStyle>
        <ItemStyle CssClass="metadatabodytext"></ItemStyle>
        <HeaderStyle CssClass="metadatabodytitle"></HeaderStyle>
        <FooterStyle CssClass="Blue"></FooterStyle>
        <Columns>
            <asp:BoundColumn DataField="LoadedFileID" HeaderText="Loaded File Id" Visible="False"></asp:BoundColumn>
            <asp:BoundColumn DataField="DataSupplierCode" HeaderText="Data Supplier Code"></asp:BoundColumn>
            <asp:BoundColumn DataField="DataSupplierName" HeaderText="Data Supplier Name"></asp:BoundColumn>
            <asp:BoundColumn DataField="Filename" HeaderText="File Name"></asp:BoundColumn>
            <asp:BoundColumn DataField="DateLoaded" HeaderText="Date Loaded"></asp:BoundColumn>
            <asp:BoundColumn DataField="LoadStatus" HeaderText="Status"></asp:BoundColumn>
        </Columns>
        <PagerStyle CssClass="Gray"></PagerStyle>
</asp:DataGrid>

code behind:

DataSet dataSet = results.DataSet;
this.fileBrowserGrid.DataSource = dataSet;
this.fileBrowserGrid.DataBind();

I want to change the Status column so that will display a hyperlink to errormessage.aspx with id as querystring value if the value is 'Failed' but stay as normal text value if its anything else.

Ideally I don't want to make changes to my stored procedures

I've been looking at RowDataBind but haven't been able to get that working.

Any ideas? Thank you!

2

There are 2 answers

4
tdelepine On BEST ANSWER

I have a solution with only the aspx and not touch the cs backend

You can predict the render of template Column. Try this I suppose that the code status that indicate failed is "failed"

<asp:TemplateColumn>
                <HeaderTemplate>
                    <b>Status </b>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:PlaceHolder ID="Ok" runat="server" Visible='<%# (Eval("LoadStatus").ToString()=="Failed"?false:true) %>'><%----%>
                        <asp:Label ID="Label1" Text='<%# Eval("LoadStatus") %>' runat="server" />
                    </asp:PlaceHolder>
                    <asp:PlaceHolder ID="Ko" runat="server" Visible='<%# (Eval("LoadStatus").ToString()=="Failed"?true:false) %>'><%----%>
                        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# string.Format("DataLoaderErrorMessage.aspx?id={0}",Eval("LoadedFileID"))%>'><%# Eval("LoadStatus") %></asp:HyperLink>
                    </asp:PlaceHolder>
                </ItemTemplate>
            </asp:TemplateColumn>
1
Dean Martin On

1) Set the AutoGenerateColumns property of the datagrid to false. 2) Create a template column instead of a bound column for the status. 3) Set the 'DataField' property for every column (Except the template column) so they know which value to display from the sql datasource. 4) Edit the template column and add a html div in there with the id divStatus

 <asp:TemplateField HeaderText="Status">
      <ItemTemplate>
           <div id="divStatus" runat="server">
           </div>
      </ItemTemplate>
 </asp:TemplateField>

Iterate through all the rows after setting the datasource of the gridview and do something like the following.

 for(int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
            {
                HtmlGenericControl divStatus = (HtmlGenericControl)fileBrowserGrid.Rows[i].FindControl("divStatus");

                if(dataSet.Tables[0].Rows[i]["LoadStatus"].ToString() != "Failed")
                     divStatus.InnerHtml = dataSet.Tables[0].Rows[i]["LoadStatus"].ToString();
                else
                     divStatus.InnerHtml = "<a href='pageURL.aspx?ID=" + dataSet.Tables[0].Rows[i]["LoadedFileID"].ToString() + "'> Failed : " + dataSet.Tables[0].Rows[i]["LoadedFileID"].ToString() + "</a>";
            }