Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Formatting Hyperlink columns in datagrid control

Formatting Hyperlink columns in datagrid control

Scheduled Pinned Locked Moved ASP.NET
csharpc++asp-netdatabase
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ranjith I
    wrote on last edited by
    #1

    Does anyone know how to custom format the hyperlink columns in datagrid control? I want to display only selected values in the cells, as hyperlink if some condition is met. VB,ASP, C#, ASP.NET, VB.NET, Oracle, SQL Server. -------------------------------------------------- Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup

    M 1 Reply Last reply
    0
    • R Ranjith I

      Does anyone know how to custom format the hyperlink columns in datagrid control? I want to display only selected values in the cells, as hyperlink if some condition is met. VB,ASP, C#, ASP.NET, VB.NET, Oracle, SQL Server. -------------------------------------------------- Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup

      M Offline
      M Offline
      Mariusz Wojcik
      wrote on last edited by
      #2

      That type of column is rendered as HyperLink control. To manipulate what is show in the row do implement the DataGrid's ItemDataBound event. Here is the small example:

      private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
      {
      if (e.Item.ItemType == ListItemType.Item ||
      e.Item.ItemType == ListItemType.AlternatingItem)
      {
      // data item represented by the DataGridItem object
      DataRowView drv = e.Item.DataItem as DataRowView;
      if (drv == null) return;

      // drv.Row represents the ROW
      // read value from "WebsiteID" column (type of int)
      int i = (int) drv.Row\["WebsiteID"\];
      
      // calculate something...
      bool b = (i % 2) == 1;
      
      // get HyperLink column object
      HyperLink hl = e.Item.Cells\[0\].Controls\[0\] as HyperLink;
      
      // set NavigateUrl
      if (!b) hl.NavigateUrl = "";
      

      }
      }

      In this example, the first DataGrid's column (index = 0) is a HyperLink column (accessor: e.Item.Cells[0]). That type of column has only one controls in the colection, type of HyperLink.
      There is a good example in MSDN, see "DataGridItemEventArgs class, about DataGridItemEventArgs class" topic. -- Mariusz 'mAv' Wójcik master e-software engineer (BPC)

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups