Databound information and Hyperlink Cell

5 posts, 0 answers
  1. Burt
    Burt avatar
    5 posts
    Member since:
    Jul 2012

    Posted 16 Apr 2012 Link to this post

    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.

  2. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 19 Apr 2012 Link to this post

    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 >>
  3. Burt
    Burt avatar
    5 posts
    Member since:
    Jul 2012

    Posted 19 Apr 2012 Link to this post

    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.

  4. Muthu
    Muthu avatar
    8 posts
    Member since:
    Oct 2012

    Posted 22 Jan 2013 Link to this post

    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
  5. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 24 Jan 2013 Link to this post

    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.
Back to Top