Problem with dynamic grids and record parameter in column renderer

  1. #1

    Problem with dynamic grids and record parameter in column renderer

    Hello!

    I am using a grid with dynamic columns and a custom column render. If the column names change with each render, the "record" parameter received by the renderer has all the fields the grid has had instead of only the current ones. Here is an example:

    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <ext:ResourcePlaceHolder ID="ResourcePlaceHolder1" runat="server" Mode="ScriptFiles" />
    
    <script type="text/javascript">
        Ext.net.GridPanel.prototype.doSelection = Ext.net.GridPanel.prototype.doSelection.createInterceptor(function () {
            this.getSelectionModel();
        });
    
        function MyRenderer(value, metadata, record, rowIndex, colIndex, store) {
            return record.fields.length;
        }
    
    </script>
    
    <script runat="server">
    
        Random rnd = new Random();
    
        [DirectMethod]
        public void Prepare_Click(object sender, DirectEventArgs e)
        {
            PrepareGrid(myGrid);
        }
    
        private void PrepareGrid(GridPanel grid)
        {
            // Add columns to grid.
            var columns = new string[] { "Text1_" + rnd.Next(9), "Num1_" + rnd.Next(9), "Text2_" + rnd.Next(9), "Num2_" + rnd.Next(9), "Comments" };
    
            foreach (string col in columns)
            {
                Column newCol = new Column { Header = col, DataIndex = col, Width = 120, MenuDisabled = true, Sortable = false };
                grid.ColumnModel.Columns.Add(newCol);
            }
    
            grid.Controls.RemoveAt(0);
            grid.Render();
    
            // Add records to store reader.        
            RecordField newRecord1 = new RecordField(columns[0], RecordFieldType.String);
            RecordField newRecord2 = new RecordField(columns[1], RecordFieldType.Int);
            RecordField newRecord3 = new RecordField(columns[2], RecordFieldType.String);
            RecordField newRecord4 = new RecordField(columns[3], RecordFieldType.Int);
            this.strDemo.AddField(newRecord1);
            this.strDemo.AddField(newRecord2);
            this.strDemo.AddField(newRecord3);
            this.strDemo.AddField(newRecord4);
    
            // Create data table
            DataTable data = new DataTable();
            data.Columns.Add("Date", typeof(DateTime));
            data.Columns.Add(columns[0], typeof(string));
            data.Columns.Add(columns[1], typeof(int));
            data.Columns.Add(columns[2], typeof(string));
            data.Columns.Add(columns[3], typeof(int));
            data.Columns.Add("Comments", typeof(string));
    
            data.Rows.Add(DateTime.Now, "Line 1 " + rnd.Next(9), 10, "Some text " + rnd.Next(9), 20, "Correct");
            data.Rows.Add(DateTime.Now.AddDays(1).AddHours(1), "Line 2 " + rnd.Next(9), 15, "More text " + rnd.Next(9), 25, "Wrong");
    
            this.strDemo.DataSource = data;
            this.strDemo.DataBind();
        }
    
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Dynamic Grid - Record Fields Problem</title>
    </head>
    <body>
        <form id="Form1" runat="server">
            <ext:ResourceManager ID="ResourceManager1" runat="server" />
    
            <ext:Store ID="strDemo" runat="server">
                <Reader>
                    <ext:JsonReader IDProperty="Date">
                        <Fields>
                            <ext:RecordField Name="Date" Type="Date" />
                            <ext:RecordField Name="Comments" Type="String" />
                        </Fields>
                    </ext:JsonReader>
                </Reader>
            </ext:Store>
    
            <ext:Viewport ID="Viewport1" runat="server" Layout="border">
                <Items>
    
                    <ext:Panel runat="server" Region="West" Width="100">
                        <Items>
                            <ext:Button runat="server" ID="btnPrepare" Text="Load">
                                <DirectEvents>
                                    <Click OnEvent="Prepare_Click" />
                                </DirectEvents>
                            </ext:Button>
                        </Items>
                    </ext:Panel>
    
                    <ext:Panel runat="server" Region="Center">
                        <Items>
                            <ext:GridPanel ID="myGrid" runat="server" StoreID="strDemo" AutoHeight="true" EnableColumnMove="false" EnableColumnResize="false">
                                <ColumnModel ID="ColumnModel1" runat="server">
                                    <Columns>
                                        <ext:Column Header="Field Count">
                                            <Renderer Fn="MyRenderer" />
                                        </ext:Column>
                                        <ext:DateColumn Header="Day" DataIndex="Date" Format="MM/dd/yyyy" MenuDisabled="true" Sortable="false" />
                                        <ext:DateColumn Header="Time" DataIndex="Date" Format="HH:mm:ss" MenuDisabled="true" Sortable="false" />
                                    </Columns>
                                </ColumnModel>
                                <View>
                                    <ext:GridView />
                                </View>
                            </ext:GridPanel>
                        </Items>
                    </ext:Panel>
    
                </Items>
            </ext:Viewport>
        </form>
    </body>
    </html>
    Each time you press Load, new random column names are rendered and new data is loaded, but in record.fields (as well as store.fields) all previous dynamic fields are still present. This is shown in the "Field Count" column, which has the renderer. The main problem is then that the colIndex parameter, which corresponds to the actual column model, doesn't match with this list of fields and I can't know what column it's actually rendering.

    Summarizing, the datasource table, the store and the column model all have the appropriate columns, but the fields in the record and the store parameters sent to the renderer keep the old ones.

    Am I missing clearing something? I couldn't find anything wrong from the code behind.
    Is there a way to prevent this? It happens with all 1.x versions I have tested.

    Thanks and regards,

    Andrew
    Last edited by ALobpreis; May 28, 2014 at 12:42 PM.
  2. #2
    Could this issue be reproduced? Or is it due to some error in my code? For the moment what I did was "convert" with some loops the received colIndex to the one that corresponds to the record fields received as parameter.

    Thanks and regards,

    Andrew
  3. #3
    I still have problems with this. I only get the colIndex and have no way to know which column is actually being rendered; the index doesn't belong to the field list.

Similar Threads

  1. [CLOSED] Custom parameter in grid column Renderer
    By alscg in forum 2.x Legacy Premium Help
    Replies: 6
    Last Post: Dec 16, 2013, 3:01 AM
  2. [CLOSED] Renderer getting null value (first parameter) after refresh
    By jmcantrell in forum 1.x Legacy Premium Help
    Replies: 4
    Last Post: Jun 05, 2013, 2:40 PM
  3. [CLOSED] when gridpanel column apply renderer.format , renderer.fn not work
    By mis@adphk.com in forum 2.x Legacy Premium Help
    Replies: 1
    Last Post: Mar 12, 2013, 10:35 AM
  4. [CLOSED] GridPanel: Problem with Dynamic Column
    By nhg_itd in forum 1.x Legacy Premium Help
    Replies: 7
    Last Post: Oct 19, 2011, 9:47 AM
  5. Two Grids - submit additional parameter [0.82]
    By roszman in forum 1.x Help
    Replies: 2
    Last Post: Jun 24, 2010, 7:11 AM

Tags for this Thread

Posting Permissions