Hi all,
I have data grid:
In that i have to show different hyperlinks colors with type of memberships.
If gold member then Hyperlink is Green
If Silver member then Hyperlink is Red.
otherwise the Hyperlink shoudl be Blue !!!
How to achieve this is Data grid!!!.

Dani AI

Generated

— both and pointed you in the right directions (use a template column or prepare the data first). Below are two concrete, practical patterns you can use right away, plus a few troubleshooting tips.

Server-side (flexible): put an <asp:HyperLink runat="server" ID="lnkMember"> inside a TemplateColumn and set its class in ItemDataBound. This keeps presentation logic out of the database and is easy to extend (icons, tooltips, conditional navigation).

protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var drv = (DataRowView)e.Item.DataItem;
        var type = drv["MemberType"].ToString();
        var lnk = (HyperLink)e.Item.FindControl("lnkMember");
        lnk.Text = drv["DisplayName"].ToString();
        lnk.NavigateUrl = drv["ProfileUrl"].ToString();
        lnk.CssClass = type == "Gold" ? "member-gold"
                      : type == "Silver" ? "member-silver"
                      : "member-regular";
    }
}

Preprocess the DataTable (fast at bind): add a computed column (e.g., LinkClass) when you load data and fill it with the desired class name. Bind that column to the hyperlink's CssClass in your TemplateColumn. This offloads per-row logic to a single loop before binding, which can be a tiny win on large result sets.

Common pitfalls and tips:

  • Make sure runat="server" is set and the control ID matches FindControl.
  • Wire the ItemDataBound/RowDataBound event on the grid.
  • Define CSS for normal/visited/hover states and prefer semantic class names (e.g., member-gold) so you don’t rely on browser defaults.
  • Consider accessibility: color alone is not always sufficient—use labels or icons and ensure contrast.

Use the server-side pattern for complex rules, or preprocessing when you want simpler templates and slightly faster binds.

Recommended Answers

All 2 Replies

try changing the column that has the hyperlink to a template column. then you can go into the (Databindings) property of the hyperlink and set the cssclass based on the type of membership.

For example, add these css classes to your css style sheet:

.gold a:link {color:green;}
.silver a:link {color: red;}
etc...

then you can either add a column to your data table to hold the type of membership or determine it programatically.

for example you can put this in the databinding for the hyperlink:
DataBinder.Eval(Container, "DataItem.memberType")

hope that made sense and helped!

-SelArom

Hi Madhusudhan,

Your question inspired me to write some in-depth articles on the subject. :eek:

I have dealt with this same problem before. I ultimately created several custom template columns which satisfy most needs.

As I demonstrate in the second article you basically have two choices:

1) Perform some preprocessing on your DataTable after filling it from the database and before binding it to the DataGrid. For example, create another column, loop over all the rows and fill the added column with the HTML you want to display. Then you can bind that column to a simple BoundColumn on the Grid.

2) Use TemplateColumns where you have total control. Then once you define your DataGrid columns, there is no preprocessing of your DataTable required. You can bind the table directly to the grid after reading from the database.

If you need a quick and dirty solution, use 1), if you expect to run into this problem again, use 2).

For all the details and my custom link template column, check out the articles.

Cheers,
- Steve

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.