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

Databound information and Hyperlink Cell

4 Answers 220 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Burt
Top achievements
Rank 1
Burt asked on 16 Apr 2012, 08:02 PM
Hi,

I started trying your RadControls specifically for the use of the GridView hierarchy, but lately have noticed a lot of complications when trying to make the transition from regular DataGridView to RadGridView. One such example:

I have a RadGridView bound to a table on a SQL Server database. Retrieving the information is fine, however, I want to make some cells hyperlinks. Previously, I would write something like this to change my cells from regular TextBoxCells to HyperlinkCells after the binding completed:

for each (Windows::Forms::DataGridViewColumn^ column in dgv->Columns)
{
    if(column->HeaderText == "Something"))
    {                      
        for each (DataGridViewRow^ row in dgv->Rows)
        {
            row->Cells[column->Index] = gcnew DataGridViewLinkCell();
        }
    }
}

But I notice there isn't any GridViewHyperlinkCell class or anything similar. How would I go about replicating this behavior? The entire column can be HyperLinkCells. I understand that you can make the entire column of type GridViewHyperLinkColumn, but how would I achieve this dynamically?


EDIT: To accomplish the functionality I described above, I did the following:
  • Set the GridView property "AutoGenerateColumns" to False
  • Manually added columns w/ desired column type and set the "FieldName" property to the corresponding column header name in my database table. This directly reflected the table structure in my database.
  • Set the GridView's "DataSource" to the bindingSource I use to retrieve the table from the database.

This takes the ease out of setting and forgetting, but I couldn't figure out an alternate way.

4 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 19 Apr 2012, 02:56 PM
Hi Burt,

Thank you for writing.

RadGridView control does not support this functionality in this way. You must add GridViewHyperLinkColumn to the Columns collection and then change the values for that cell using the row API:

row->Cells[column->Index].Value = "www.google.com";

The other solution is the binding mechanism as implemented in your application.

I hope this helps. Let me know if you need further assistance.

Greetings,
Julian Benkov
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Burt
Top achievements
Rank 1
answered on 19 Apr 2012, 03:12 PM
Yeah, that's how I implemented it, as noted in the edit of my post.

The problem with doing it as you suggested, though, is needing to generate a static design of the RadGridView instead of letting the database drive the structure. Oh well.

0
Muthu
Top achievements
Rank 1
answered on 23 Jan 2013, 04:54 AM
Hi Julian Benkov,
                       I have a column in Grid. I want some of the cells in that column to be displayed as hyperlink value and remaining as normal text. Is it possible?


Thanks,
Muthu
0
Julian Benkov
Telerik team
answered on 24 Jan 2013, 11:25 AM
Hello Muthu,

You can use the CellFormatting and CellClick events to implement this functionality. Here is a sample:
using System.Diagnostics;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
namespace Lab.Grid
{
    public partial class GridCellFormattingForm : Form
    {
        private RadGridView gridView = new RadGridView();
 
        public GridCellFormattingForm()
        {
            InitializeComponent();
 
            gridView.Dock = DockStyle.Fill;
            gridView.Parent = this;
            gridView.CellFormatting += gridView_CellFormatting;
            gridView.CellClick += gridView_CellClick;
 
            gridView.ColumnCount = 1;
            gridView.RowCount = 10;
            gridView.Columns[0].DisableHTMLRendering = false;
            gridView.BestFitColumns();
        }
 
        void gridView_CellClick(object sender, GridViewCellEventArgs e)
        {
            GridCellElement cell = gridView.TableElement.GetCellElement(e.Row, e.Column);
            if(cell != null && cell.Text.Contains("<html>"))
            {
                string []elements = cell.Text.Split('<');
                elements = elements[2].Split('\'');
                string target = elements[1];
                Process.Start(target);
            }
        }
 
        void gridView_HyperlinkOpening(object sender, HyperlinkOpeningEventArgs e)
        {
            System.Console.WriteLine("Open");
        }
 
        void gridView_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.RowIndex % 2 == 0)
            {
                e.CellElement.Text = "<html><a href='http://www.telerik.com'>Telerik</a></html>";
                 
            }
            else
            {
                e.CellElement.Text = "CellText";
            }
        }
    }
}

I hope this helps. Feel free to ask if you have any additional questions.

Greetings,
Julian Benkov
the Telerik team
Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
Tags
GridView
Asked by
Burt
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Burt
Top achievements
Rank 1
Muthu
Top achievements
Rank 1
Share this question
or