Programatic Grid creation with RadToolTip on column

4 posts, 0 answers
  1. Joe
    Joe avatar
    11 posts
    Member since:
    Sep 2007

    Posted 24 Dec 2008 Link to this post

    We are creating a Grid dynamically based upon an ad-hoc definition of selected columns and data sources by the end-user.   We have a number of columns which we need to offer the user "ToolTip" information which is database driven and specific to the value in the row.

    I've been looking at building a new class which implements ITemplate and adds the field value (using a label) and attaches a RadToolTip to the label in the cell, but it seems like I have to implement the formatting/aggregates/etc on my own.

    Another thought I had would be to use an BoundColumn, but override the DataBinding event (or to add an additional event to the firing of DataBind) and attach the tooltip there, but it seems like I can't.   We have source code access and I can see how it's done behind the scenes, but I'm just not sure where to do it and still use the released controls.

     

    Right now our columns inherit from the base GridColumns (GridBoundColumn, GridHyperlinkColumn) and get their values on the fly from from our settings system.   Here's an example:

     

     

     

            public class BoundGridColumn : Telerik.Web.UI.GridBoundColumn  
        {  
     
            public BoundGridColumn()  
            {  
     
            }  
     
            public BoundGridColumn(string column)  
            {  
                  
                    this.DataField = column;  
                    this.UniqueName = column;  
                    this.SortExpression = column;  
                    this.HeaderText = Utility.GetSetting(column + ".Header");  
                  
     
                    if (Utility.GetSetting(column + ".Width").Length > 0)  
                    {  
     
                        int _width = Convert.ToInt32(Utility.GetSetting(column + ".Width"));  
     
                        this.HeaderStyle.Width = Unit.Pixel(_width);  
                        this.FooterStyle.Width = Unit.Pixel(_width);  
     
                        this.ItemStyle.Width = Unit.Pixel(_width);  
                    }  
                    else  
                    {  
                        this.HeaderStyle.Width = Unit.Pixel(100);  
                        this.FooterStyle.Width = Unit.Pixel(100);  
                        this.ItemStyle.Width = Unit.Pixel(100);  
                    }  
     
                    if (Utility.GetSetting(column + ".Item.Align").Length > 0)  
                        this.ItemStyle.HorizontalAlign = (HorizontalAlign)Enum.Parse(typeof(HorizontalAlign) , (Utility.GetSetting(column + ".Item.Align")));  
     
                    if (Utility.GetSetting(column + ".Footer.Align").Length > 0)  
                        this.FooterStyle.HorizontalAlign = (HorizontalAlign)Enum.Parse(typeof(HorizontalAlign) , (Utility.GetSetting(column + ".Footer.Align")));  
     
                    if (Utility.GetSetting(column + ".Header.Align").Length > 0)  
                        this.HeaderStyle.HorizontalAlign = (HorizontalAlign)Enum.Parse(typeof(HorizontalAlign) , (Utility.GetSetting(column + ".Header.Align")));  
     
     
                    // = Unit.Pixel(_width);  
     
                    if (Utility.GetSetting(column + ".Visible").Length > 0)  
                        this.Visible = Convert.ToBoolean(Utility.GetSetting(column + ".Visible"));  
     
                    this.DataFormatString = Utility.GetSetting(column + ".Format");  
                    this.DataType = System.Type.GetType(Utility.GetSetting(column + ".DataType"));  
                    if (Utility.GetSetting(column + ".Aggregate").Length > 0)  
                        this.Aggregate = (GridAggregateFunction)Enum.Parse(typeof(GridAggregateFunction) , Utility.GetSetting(column + ".Aggregate"));  
                    if (Utility.GetSetting(column + ".AllowFiltering").Length > 0)  
                        this.AllowFiltering = Convert.ToBoolean(Utility.GetSetting(column + ".AllowFiltering"));  
     
                    this.Groupable = true;  
                    this.ItemStyle.Wrap = false;  
                  
                      
     
                }  
     


    So where/how can I attach a value specific tooltip to this column

     

  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 30 Dec 2008 Link to this post

    Hi,

    Here is an example how to achieve your goal:

    using System; 
    using System.Data; 
    using System.Configuration; 
    using System.Collections; 
    using System.Web; 
    using System.Web.Security; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 
    using System.Web.UI.WebControls.WebParts; 
    using System.Web.UI.HtmlControls; 
    using Telerik.Web.UI; 
    using System.Collections.Generic; 
     
    public partial class _Default : System.Web.UI.Page 
        public void Page_Init(object sender, EventArgs e) 
        { 
            RadGrid RadGrid1 = new RadGrid(); 
            RadGrid1.ID = "RadGrid1"
            RadGrid1.AllowSorting = true
            RadGrid1.AutoGenerateColumns = false
            RadGrid1.MasterTableView.Columns.Add(new MyColumn("ID")); 
            RadGrid1.MasterTableView.Columns.Add(new MyColumn("Name")); 
            RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource); 
            PlaceHolder1.Controls.Add(RadGrid1); 
        } 
     
        void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
        { 
            ((RadGrid)source).DataSource = MyData; 
        } 
     
        public List<MyObject> MyData 
        { 
            get  
            { 
                List<MyObject> list = new List<MyObject>(); 
     
                for (var i = 0; i < 10; i++) 
                { 
                    list.Add(new MyObject(i, String.Format("Name{0}", i.ToString()))); 
                } 
     
                return list; 
            } 
        } 
     
        public class MyObject 
        { 
            public MyObject(int id, string name) 
            { 
                ID = id; 
                Name = name; 
            } 
     
            public int ID { getset; } 
            public string Name { getset; } 
        } 
     
        public class MyColumn : GridBoundColumn 
        { 
            public MyColumn(string dataField) 
            { 
                DataField = HeaderText = SortExpression = UniqueName = dataField; 
            } 
     
            public override void InitializeCell(TableCell cell, int columnIndex, GridItem inItem) 
            { 
                base.InitializeCell(cell, columnIndex, inItem); 
     
                if (inItem is GridDataItem) 
                { 
                    CreateToolTip(cell, false); 
                    cell.DataBinding += new EventHandler(cell_DataBinding); 
                } 
            } 
     
            void cell_DataBinding(object sender, EventArgs e) 
            { 
                CreateToolTip((TableCell)sender, true); 
            } 
     
            public void CreateToolTip(TableCell cell, bool shouldDataBind) 
            { 
                GridDataItem item = (GridDataItem)cell.Parent; 
                string ID = String.Format("{0}_{1}", item.ItemIndexHierarchical, DataField); 
                RadToolTip toolTip = (RadToolTip)item.FindControl(ID); 
                if (toolTip == null
                { 
                    string value = FormatDataValue(DataBinder.Eval(item.DataItem, DataField), item); 
     
                    Label Label1 = new Label(); 
                    Label1.Text = value; 
                    Label1.ID = String.Format("{0}_Label", ID); 
                    cell.Controls.Add(Label1); 
     
                    toolTip = new RadToolTip(); 
                    toolTip.ID = String.Format("{0}_{1}", item.ItemIndexHierarchical, DataField); 
                    toolTip.TargetControlID = Label1.ID; 
     
                    if (shouldDataBind) 
                    { 
                        toolTip.Text = String.Format("ToolTip for ID: <b>{0}</b> ", value); 
                    } 
     
                    cell.Controls.Add(toolTip); 
                } 
            } 
        } 
     


    Best wishes,
    Vlad
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Joe
    Joe avatar
    11 posts
    Member since:
    Sep 2007

    Posted 19 Nov 2009 Link to this post

    When we use the above code, on postback the values in the field disappear and the Text property is blank.   How can I persist the GridBoundColumn's data.
  5. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 25 Nov 2009 Link to this post

    Hello JOe,

    Please find attached a modified version of the sample solution and let me know if it works for you.

    Sincerely yours,
    Iana
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top