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

Cell Formatting and cell events

2 Answers 241 Views
GridView
This is a migrated thread and some comments may be shown as answers.
License 1EDISource
Top achievements
Rank 1
License 1EDISource asked on 29 May 2008, 05:05 PM
So.  I saw the post about adding a hypertext link to a cell and that it will be added in future versions.  I'm trying to find a workaround for this for time being. 

I have tried to use the GridViewCommandColumn type, but I don't want the cell to look like a button.  I can't find where I can change that either using a style builder or just by getting to the appropriate style properties of the cell or column.

I almost have exactly what I want by adding a mouse enter, leave and click event to the CellElement.  The problem that I have is that the only time that I can find to get the CellElement object is during the CellFormatting event.  Unfortunately, by changing the Font (to underline or not on MouseEnter and MouseLeave) is causing the CellFormatting event to fire which causes me to add handlers again.

private void grid_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.CellElement.ColumnInfo.HeaderText == "MYField")
            {
                e.CellElement.Click         += new EventHandler(MYField_Click);
                e.CellElement.MouseEnter    += new EventHandler(MYField_MouseEnter);
                e.CellElement.MouseLeave    += new EventHandler(MYField_MouseLeave);
            }
        }

Any thoughts on where, other than the CellFormatting event I can get a handle the actual CellElements?

2 Answers, 1 is accepted

Sort by
0
Accepted
Jack
Telerik team
answered on 30 May 2008, 04:41 PM
Hello Brian Bizzi,

Thank you for this question.

In its Q1 2008 SP1 release RadGridView offers the possibility to have custom cells. This is achieved by writing a custom class, a descendant of the GridCellElement class and processing the CreateCell event in the new class.

Refer to the code below:

this.radGridView1.CreateCell += new GridViewCreateCellEventHandler(radGridView1_CreateCell); 
 
void radGridView1_CreateCell(object sender, GridViewCreateCellEventArgs e) 
    if (e.Column is GridViewDataColumn && ((GridViewDataColumn)e.Column).FieldName == "Link"
    { 
        e.CellType = typeof(HyperlinkCellElement); 
    } 
     
public class HyperlinkCellElement : GridDataCellElement 
    { 
        Font font; 
 
        public HyperlinkCellElement(GridViewColumn column, GridRowElement row): 
            base(column, row) 
        { 
            font = new Font(SystemFonts.DialogFont, FontStyle.Underline); 
        } 
 
        public override void UpdateInfo() 
        { 
            base.UpdateInfo(); 
            this.ForeColor = Color.Blue; 
            this.Font = font; 
        } 
 
        protected override void OnMouseDown(MouseEventArgs e) 
        { 
            base.OnMouseDown(e); 
            Process.Start("http://www.microsoft.com"); 
        } 
    } 
 

I hope this helps. Do not hesitate to contact us if you have other questions.

Best wishes,
Jack
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
License 1EDISource
Top achievements
Rank 1
answered on 02 Jun 2008, 01:48 PM
Jack -

Thanks for the help.  That's exactly what I was looking for!
Tags
GridView
Asked by
License 1EDISource
Top achievements
Rank 1
Answers by
Jack
Telerik team
License 1EDISource
Top achievements
Rank 1
Share this question
or