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

How to access grid row data in GridTemplateColumn databinding event

2 Answers 300 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Amod
Top achievements
Rank 1
Amod asked on 19 Jan 2016, 01:12 PM

I have a RadGrid server control completely developed from code-behind. The columns of the grid are bound through a config xml file. and need zero declarative syntax. I have 3 columns in the grid - 2 are GridBoundColumn and 1 is a template column. The grid is bound to a List<>. The template column can be anything - asp-label or hyperlink etc. I want to access the data-value for this column in the template binding event by using the column's UniqueName. I am stuck as I am not getting the value in that column in the data-binding event. Please refer code below. Any help is appreciated.

public class PcsGrid : Telerik.Web.UI.RadGrid
{
    public PcsGrid()
    {
        this.AutoGenerateColumns = false;
        GridViewConfiguration gvc = SelectViewConfiguration();
         
        // this method generates all the columns at runtime        
        BuildColumns(gvc.View.Columns);
    }
     
    // HyperlinkColumn() is 1 such column created at runtime. this is invoked inside BuildColumns.
    private Telerik.Web.UI.GridTemplateColumn HyperlinkColumn(GridViewConfigurationViewColumn col)
    {
        GridTemplateColumn gtc = new GridTemplateColumn();
        gtc.UniqueName = col.UniqueName;
        gtc.HeaderText = col.HeaderText;
        gtc.DataType = Type.GetType("System.String");
        gtc.DataField = col.DataField;
        gtc.ItemTemplate = new TemplateColumns.ActionColumn(col.UniqueName);
        return gtc;
    }
     
    // a template column class
    class ActionColumn : ITemplate
    {
        Label lc = null;
        string c_sUniqueName = null;
         
        //i am passing the unique-column name
        public ActionColumn(string p_sUniqueName)
        {
            this.c_sUniqueName = p_sUniqueName;
        }
 
        public void InstantiateIn(System.Web.UI.Control container)
        {
            lc = new Label();
            lc.ID = "lCity";
            lc.DataBinding += lc_DataBinding;
            container.Controls.Add(lc);
        }
 
        void lc_DataBinding(object sender, EventArgs e)
        {
            Label oSender = (Label)sender;
            GridDataItem container = (GridDataItem)oSender.NamingContainer;
            // if I debug here I see the data in the container. I dont want to cast it to the model object because this is a common server control not specific to a functionality
            TableCell tc = container[c_sUniqueName]; // unique name. none of the properties I tried return the data-value.
            Label l = (Label)tc.Controls[0];
            //this is blank and returns nothing.
             
        }
    }      
}

 

I also referred to article - http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows

But no luck.

Please help.

2 Answers, 1 is accepted

Sort by
0
Accepted
Eyup
Telerik team
answered on 22 Jan 2016, 08:52 AM
Hi Amod,

You can try using the DataBinder.Eval(container.DataItem, dataField) approach as demonstrated in the attached web site sample.

However, please keep in mind that creating custom controls is beyond our support scope even if they are inherited directly from Telerik UI controls. If you happen to come across the need to implement something like this, you can use UserControls.

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Amod
Top achievements
Rank 1
answered on 23 Jan 2016, 05:10 AM

Eyup,

Perfect. Thanks a ton for your help. This works great. I really appreciate your time and efforts.

 

Tags
Grid
Asked by
Amod
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Amod
Top achievements
Rank 1
Share this question
or