0

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!

Tevo D
  • 3,351
  • 21
  • 28
ShufflerShark
  • 377
  • 1
  • 4
  • 20

2 Answers2

2

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>
ShufflerShark
  • 377
  • 1
  • 4
  • 20
tdelepine
  • 1,986
  • 1
  • 13
  • 19
  • Thanks for the response. This is almost working as it creates the string okbut it does not seem to be reading the LoadStatus Value as all that happens is that all appear as text only as defaulting to true for Label and false for hyperlink – ShufflerShark Aug 28 '13 at 14:18
  • I'm sorry i'm french and my english is not fluent. I don't understand what happen. It's on this part of code your question ? <%# (Eval("LoadStatus")=="failed"?false:true) %> – tdelepine Aug 28 '13 at 14:45
  • I got it working and all that you where missing was .ToString() after the Eval("LoadStatus"). I've updated your answer to show this and marked you answer as correct. Thanks. – ShufflerShark Aug 28 '13 at 14:45
  • Yes he missing it, sorry for this forgot – tdelepine Aug 28 '13 at 14:48
0

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>";
            }
Dean Martin
  • 1,223
  • 3
  • 18
  • 27
  • Thanks for response. This looks like it could work but is for GridView rather than DataGrid (its an old site). I may update if I have too but trying to avoid that for now to avoid having to play about with css – ShufflerShark Aug 28 '13 at 14:29