Custom Column Render Problem

1 posts, 0 answers
  1. Delvis
    Delvis avatar
    18 posts
    Member since:
    Mar 2012

    Posted 02 Mar 2015 Link to this post

    I created my own  "Template" column. Everything works ok. But when I add
    a child hierarchy and expand any row, the values of that column in the
    father grid are lost.

    Any suggestions on this?

    I added part of my code to show the situation.

    Code:

    public class MyGridTemplateColumn : GridTemplateColumn
    {
        public override void Initialize()
        {
            base.Initialize();

            HeaderStyle.Width = Unit.Pixel(105);
            HeaderText = "State";
            UniqueName = "StateCol";

            DataField = "StateId";
            DataType = typeof(byte);

            var _dataSource = new List<KeyValuePair<int, string>>
            {
                new KeyValuePair<int, string>(1, "Active"),
                new KeyValuePair<int, string>(2, "Deleted"),
                new KeyValuePair<int, string>(3, "Inactive"),
                new KeyValuePair<int, string>(6, "Other")
            };

            ItemTemplate = new MyItemTemplate(this.DataField, _dataSource);
            EditItemTemplate = new MyEditItemTemplate(this.DataField, _dataSource);
        }

        public override GridColumn Clone()
        {
            var res = new MyGridTemplateColumn();
            res.CopyBaseProperties(this);

            return res;
        }

        private class MyItemTemplate : ITemplate
        {
            private readonly string _dataField;
           
    private readonly List<KeyValuePair<int, string>>
    _dataSource = new List<KeyValuePair<int, string>>();

            public MyItemTemplate(string dataField, List<KeyValuePair<int, string>> origenDatos)
            {
                _dataField = dataField;
                _dataSource = origenDatos;
            }

            public void InstantiateIn(System.Web.UI.Control container)
            {
                var lControl = new LiteralControl { ID = _dataField + "_lControl" };
                lControl.DataBinding += lControl_DataBinding;
                container.Controls.Add(lControl);
            }

            private void lControl_DataBinding(object sender, EventArgs e)
            {
                var l = (LiteralControl)sender;
                var value = l.NamingContainer.GetFieldValue("DataItem." + _dataField);
                if (value != null)
                    l.Text = _dataSource.FirstOrDefault(s=>s.Key == (int)value).Value;
            }
        }

        private class MyEditItemTemplate : IBindableTemplate
        {
            private readonly string _dataField;
           
    private readonly List<KeyValuePair<int, string>>
    _dataSource = new List<KeyValuePair<int, string>>();

            public MyEditItemTemplate(string dataField, List<KeyValuePair<int, string>> origenDatos)
            {
                _dataField = dataField;
                _dataSource = origenDatos;
            }

            public void InstantiateIn(System.Web.UI.Control container)
            {
                var rcBox = new RadComboBox
                {
                    ID = _dataField + "_RadComboBox",
                    DataTextField = "Value",
                    DataValueField = "Key",
                    DataSource = _dataSource
                };

                rcBox.DataBinding += rcBox_DataBinding;
                container.Controls.Add(rcBox);
            }

            public IOrderedDictionary ExtractValues(Control container)
            {
                var dictionary = new OrderedDictionary
                {
                    {_dataField, (container.FindControl(_dataField + "_RadComboBox") as RadComboBox).SelectedValue}
                };
                return dictionary;
            }

            private void rcBox_DataBinding(object sender, EventArgs e)
            {
                var rcBox = (RadComboBox)sender;
                var value = rcBox.NamingContainer.GetFieldValue("DataItem." + _dataField);
                rcBox.SelectedValue = value != null ? value.ToString() : string.Empty;
            }
        }
    }

    Usage:

    <telerik:RadGrid
    ID="RadGrid1" runat="server" DataSourceID="LinqDataSource1"
    AllowPaging="True" AutoGenerateColumns="False" Width="50%">
        <MasterTableView DataKeyNames="ENTUserAccountId" DataSourceID="LinqDataSource1" PageSize="5">
            <Columns>
                <telerik:GridBoundColumn DataField="UserName" HeaderText="UserName" UniqueName="UserName"/>
                <telerik:GridBoundColumn DataField="Email" HeaderText="Email" UniqueName="Email"/>
                <a:MyGridTemplateColumn />
            </Columns>
            <DetailTables>
                <telerik:GridTableView runat="server" DataSourceID="LinqDataSource2" DataKeyNames="ENTUserAccountId">
                    <ParentTableRelation>
                        <telerik:GridRelationFields DetailKeyField="ENTUserAccountId" MasterKeyField="ENTUserAccountId" />
                    </ParentTableRelation>
                    <Columns>
                        <telerik:GridTemplateColumn DataField="ENTRoleId" HeaderText="Rol" UniqueName="ENTRoleId">
                            <ItemTemplate>
                               
    <asp:Label ID="ENTRoleIdLabel" runat="server" Text='<%#
    Eval("ENTRole.RoleName") %>'></asp:Label>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                    </Columns>                 
                </telerik:GridTableView>
            </DetailTables>
        </MasterTableView>
    </telerik:RadGrid>

    <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="TelerikWebApp1.DataClasses1DataContext"
        EntityTypeName="" TableName="ENTUserAccount"/>
            
    <asp:LinqDataSource ID="LinqDataSource2" runat="server" ContextTypeName="TelerikWebApp1.DataClasses1DataContext"
        EntityTypeName="" TableName="ENTRoleUserAccount" Where="ENTUserAccountId == @ENTUserAccountId">
            <WhereParameters>
                <asp:SessionParameter Name="ENTUserAccountId" Type="Int32" />
            </WhereParameters>
    </asp:LinqDataSource>  



Back to Top