Introduction
Using Aspose.Grid.Desktop, it is also possible to add hyperlinks to simple values stored in cells of a worksheet. Let's say that in some cells, you might have some values that you would like to link with more detailed information on a webpage. In that case, it would be desirable to add a hyperlink to that cell so that if a user click on the cell then he would be directed to that webpage. In this topic, we will explain that how developers can add and manipulate hyperlinks in their worksheets.
Managing Hyperlinks in a Worksheet
Adding Hyperlinks
To add a hyperlink to a cell using Aspose.Grid.Desktop, please follow the steps below:
- Add Aspose.Grid.Desktop control to your Form
- Access any desired Worksheet
- Access a desired Cell in the worksheet that will be hyperlinked
- Add some value to the cell to be hyperlinked
- Add Hyperlink to the worksheet by specifying the cell name on which the hyperlink would be applied
NOTE: Hyperlinks collection in the Worksheet object provides an overloaded Add method. Developers can use any overloaded version of Add method according to their specific needs.
Example:
[C#]
//Accessing first worksheet of the Grid
Worksheet sheet = gridDesktop1.Worksheets[0];
//Accessing "b2" cell of the worksheet
GridCell cell = sheet.Cells["b2"];
//Modifying the width of the column of the cell
sheet.Columns[cell.Column].Width = 160;
//Adding a value to the cell
cell.Value = "Aspose Home";
//Adding a hyperlink to the worksheet containing cell name and the hyperlink
//URL with which the cell will be linked
sheet.Hyperlinks.Add("b2", "www.aspose.com");
[VB.NET]
'Accessing first worksheet of the Grid
Dim sheet As Worksheet = gridDesktop1.Worksheets(0)
'Accessing "b2" cell of the worksheet
Dim cell GridCell = sheet.Cells("b2")
'Modifying the width of the column of the cell
sheet.Columns(cell.Column).Width = 160
'Adding a value to the cell
cell.Value = "Aspose Home"
'Adding a hyperlink to the worksheet containing cell name and the hyperlink
'URL with which the cell will be linked
sheet.Hyperlinks.Add("b2", "www.aspose.com")
The output of above code snippet is displayed in the figure shown below:
|
Figure: A Hyperlink added to B2 cell
|
NOTE: If you want to add a hyperlink to a cell and want to display the hyperlink URL in the cell instead of some value then don't add any value to the cell and simply add the hyperlink to that cell. Doing so, the cell will be hyperlinked and the hyperlink URL will also be displayed in the cell as its value.
Accessing Hyperlinks
Once a hyperlink will be added to a cell, it may also be required to access and modify the hyperlink at runtime. To do so, developers can simply access the hyperlink from the Hyperlinks collection of the Worksheet by specifying the cell (using cell name or its location in terms of row and column number) to which the hyperlink is added. Once the hyperlink is accessed, developers can modify its Url at runtime.
Example:
[C#]
//Accessing first worksheet of the Grid
Worksheet sheet = gridDesktop1.Worksheets[0];
//Accessing a hyperlink added to "c3" cell (specified using its row & column number)
Hyperlink hyperlink1 = sheet.Hyperlinks[2, 2];
//Modifying the Url of the hyperlink
hyperlink1.Url = "www.aspose.com";
//Accessing another hyperlink added to "b2" cell (specified using its name)
Hyperlink hyperlink2 = sheet.Hyperlinks["b2"];
//Modifying the Url of the hyperlink
hyperlink2.Url = "www.aspose.com";
[VB.NET]
'Accessing first worksheet of the Grid
Dim sheet As Worksheet = gridDesktop1.Worksheets(0)
'Accessing a hyperlink added to "c3" cell (specified using its row & column number)
Dim hyperlink1 As Hyperlink = sheet.Hyperlinks(2, 2)
'Modifying the Url of the hyperlink
hyperlink1.Url = "www.aspose.com"
'Accessing another hyperlink added to "b2" cell (specified using its name)
Dim hyperlink2 As Hyperlink = sheet.Hyperlinks ("b2")
'Modifying the Url of the hyperlink
hyperlink2.Url = "www.aspose.com"
Removing Hyperlinks
To remove an existing hyperlink, developers can simply access a desired worksheet and then Remove hyperlink from the Hyperlinks collection of the Worksheet by specifying the hyperlinked cell (using its name or row & column number).
Example:
[C#]
//Accessing first worksheet of the Grid
Worksheet sheet = gridDesktop1.Worksheets[0];
//Removing hyperlink from "c3" cell
sheet.Hyperlinks.Remove(2, 2);
[VB.NET]
'Accessing first worksheet of the Grid
Dim sheet As Worksheet = gridDesktop1.Worksheets(0)
'Removing hyperlink from "c3" cell
sheet.Hyperlinks.Remove(2, 2)