3

I have a GridView (gv) bound to a dataset (ds). Columns[1] is bound to a field in ds named orderFilename; Columns[6] is a date field.

If Columns[6] is null, I want Columns[1] to appear as text; if Columns[6] is not null, I want Columns[1] to appear as a hyperlink, with a url ~/directory/ + orderFilename.

I have found a couple possible solutions on the Web but none seem to do what I want. Any help would be appreciated.

Taz
  • 3,718
  • 2
  • 37
  • 59
  • possible duplicate of [How to implement conditional formatting in a GridView](http://stackoverflow.com/questions/661670/how-to-implement-conditional-formatting-in-a-gridview) – Simon Mourier Aug 19 '11 at 16:50
  • Yes, it does; sorry about that. I looked around on the site and didn't find that one. – Dave Reinheimer Aug 19 '11 at 17:50

3 Answers3

4

I prefer to stay away from BoundFields specifically because the next guy needs to always seem to convert them to template fields anyways to do customizations. I would recommend the following:

Use a template field with a Literal control for your column 1:

<asp:TemplateField HeaderText="File">
    <ItemTemplate>
        <asp:Literal ID="ltFilename" runat="server" 
            OnDataBinding="ltFilename_DataBinding" />
   </ItemTemplate>
</asp:TemplateField>

Then implement the OnDataBinding for the columns control:

protected void ltFilename_DataBinding(object sender, System.EventArgs e)
{
    Literal lt = (Literal)(sender);
    if (Eval("yourColumn6Field") == DBNull.Value)
    {
        // just show a text filename
        lt.Text = Eval("orderFilename").ToString();
    }
    else
    {
        // produce the link
        lt.Text = string.Format("<a href='{0}'>{1}</a>",
             ResolveUrl("~/directory/" + Eval("orderFilename").ToString()),
             Eval("orderFilename").ToString());
    }
}

The advantage to this is you have localized the logic directly to the control. You can easily swap it out and change it around without affecting other parts of the grid accidently.

Kelsey
  • 47,246
  • 16
  • 124
  • 162
  • I'm trying to use this, but for some reason, the if statement is not reading the column field as null. Even though it says NULL in the db when I open it using SQL Server Manager, this is ending everything to the "else" block. – Dave Reinheimer Aug 19 '11 at 18:32
  • @Dave Reinheimer Oops sorry Dave, you need to compare against: `DBNull.Value` not `null` :) I have updated the answer to reflect the correct compare. – Kelsey Aug 19 '11 at 20:14
  • Found the answer here: [link](http://www.ginktage.com/2010/06/c-4-0finding-if-the-string-contains-only-whitespace-or-null/)string.IsNullOrEmpty()...appears to be working now...just saw your comment Kelsey..I'll take a look...thanks! – Dave Reinheimer Aug 19 '11 at 20:15
1

Let's say, you have added a hyperlink control in column[1], If the column[6] is not null then you can set the NavigateURL property and set the URL. In this case, it will look like a hyperlink and if column[6] is null, then you don't need to set the URL, as it will behave like text.

Off The Gold
  • 1,228
  • 15
  • 28
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • you have to be careful... It is rendered properly in IE, but in FF it shows up underlined still... so you will also need to set the style. – c0deNinja Aug 19 '11 at 16:54
0

Use a template column, and put two panels inside of it. One panel containing the link, and the other containing the text. Try something like this:

<asp:TemplateField>
    <ItemTemplate>
        <asp:Panel ID="pnlLink" runat="server" Visible='<%#Eval("SomeColumn") != null%>'>
             <asp:HyperLink ... ></asp:HyperLink>
        </asp:Panel>
        <asp:Panel ID="pnlLink" runat="server" Visible='<%#Eval("SomeColumn") = null%>'>
             <%#Eval("SomeColumn")%>
        </asp:Panel>
    </ItemTemplate>
</asp:TemplateField>

The other option, as @Muhammad Akhtar suggested, is to use a hyperlink regardless, and only set the URL when the DataField for Column[6] is not null.

James Johnson
  • 45,496
  • 8
  • 73
  • 110