This is a migrated thread and some comments may be shown as answers.

Hyperlink in VirtualGrid

3 Answers 37 Views
VirtualGrid
This is a migrated thread and some comments may be shown as answers.
Marianne
Top achievements
Rank 1
Veteran
Marianne asked on 24 Apr 2020, 06:38 PM

is it possible to drop a hyperlink object - as a custom cell, no doubt - into a virtual grid

I downloaded the example from another post in this forum but it was for a checkbox

I found a HyperlinkElement but it is calling for the specific row and column before instantiating

any assistance is greatly appreciated

3 Answers, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 29 Apr 2020, 07:57 AM

Hello, Marianne,

Your question has already been answered in the support ticket that you have opened on the same topic. Please, see our answer there for more information.

However, I am posting the solution suggested by my colleague, Lance, here as well in order the community to benefit from it.

You can follow the RadVirtualGrid Custom Cells tutorial. It will show you how to create a custom cell. Be sure to carefully read the steps numbers as they explain the fundamental information for the custom cell.

Although the tutorial is for a checkbox, you can use whatever custom element you want. If you don't already have a preferred way of rendering hyperlinks in WinForms, you could try a RadLabelElement and use the RadLabel HTML-like formatting feature.  
public class VirtualGridHyperlinkCellElement : VirtualGridCellElement
{
    private RadLabelElement aLabel;

    protected override void CreateChildElements()
    {
        base.CreateChildElements();

        aLabel = new RadLabelElement();
        this.Children.Add(aLabel);
    }

    protected override void UpdateInfo(VirtualGridCellValueNeededEventArgs args)
    {
        base.UpdateInfo(args);

        if (args.Value is string)
        {
            aLabel.Text = args.Value.ToString();
        }

        this.Text = string.Empty;
    }

    public override bool IsCompatible(int data, object context)
    {
        VirtualGridRowElement rowElement = context as VirtualGridRowElement;

        return data == 6 && rowElement.RowIndex >= 0;
    }

    public override void Attach(int data, object context)
    {
        base.Attach(data, context);

        // Subscribe to the RadLabelElement's click event when the cell is attached.
        aLabel.Click += ALabel_Click;
    }

    public override void Detach()
    {
        // Unsubscribing when the cell is detached (prevents memory leak).
        aLabel.Click -= ALabel_Click;

        base.Detach();
    }

    private void ALabel_Click(object sender, EventArgs e)
    {
        var theFilePath = "";

        Process.Start(theFilePath);
    }
}

The above code snippet relies on the fact that the cell stores the URL. Thus, when it is clicked, the process is started.

Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify and extend it in a way which suits your requirements best.

We kindly ask you to use just one thread for a specific problem to contact us. Posting the same questions numerous times slows down our response time because we will need to review and address two or more tickets instead of one. Moreover, threads are handled according to license and time of posting, so if it is an urgent problem, we suggest you use a support ticket, which would be handled before a forum thread.

Thank you for your understanding.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Marianne
Top achievements
Rank 1
Veteran
answered on 29 Apr 2020, 06:17 PM

Yes, Dess, that is my code - but I needed to make some corrections

add a private string variable called filePath to the class
set this variable in the UpdateInfo method
use the class variable in the Process.Start in the label click event handler (remove the string variable in the method - it isn't needed)
finally, surround the args value with an <a> within a <span> within an <html> putting the args value in the href property of the <a> tag

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 30 Apr 2020, 04:37 AM

Hello, Marianne,

Thank you for the provided  detailed improvements of the previously suggested code snippet.

I believe that it would be helpful for the community.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
VirtualGrid
Asked by
Marianne
Top achievements
Rank 1
Veteran
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Marianne
Top achievements
Rank 1
Veteran
Share this question
or