The official release of Ext.NET Mobile is now available. Ext.NET Mobile is an ASP.NET component framework for building Phone and Tablet specific mobile web applications. Read More

[CLOSED] GridPanel get Column in ColumnModel by ID

  1. #1

    [CLOSED] GridPanel get Column in ColumnModel by ID

    Hi,

    is there some way instead of only using index

    this.GridPanel1.ColumnModel.SetHidden(2, true);
    to get the correct Column by ID (without looping) like

    this.GridPanel1.ColumnModel.SetHidden("ColumnID", true);
    
    this.GridPanel1.ColumnModel.Columns["ColumnID"].Hidden = true;
    
    this.GridPanel1.ColumnModel.Columns[this.GridPanel1.ColumnModel.findColumnIndex("ColumnID")].Hidden = true;
    if not, whats the best way of looping over the set of columns on runtime?
    If i use this code:

    foreach (var col in GridPanel1.ColumnModel.Columns)
    {
        if (col.ColumnID.Equals("ID_XYZ"))
        {
    I receive this Exception: Unable to cast object of type 'Ext.Net.CheckColumn' to type 'Ext.Net.Column'.

    Kind regards.
    Last edited by Daniil; Jan 09, 2011 at 12:06 PM. Reason: [CLOSED]
  2. #2
    Hi,

    I would suggest you to use own client-side function like .setHiddenById() and call when it needs.

    It can look something like this.

    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                Store store = this.GridPanel1.GetStore();
                store.DataSource = new object[] 
                { 
                    new object[] { "test11", "test12", "test13" },
                    new object[] { "test12", "test22", "test23" },
                    new object[] { "test13", "test32", "test33" }
                };
                store.DataBind();
            }
        }
    
        protected void SetHiddenById(object sender, DirectEventArgs e)
        {
            string columnId = e.ExtraParams["columnId"];
            X.Call("setHiddenById", new JRawValue(this.GridPanel1.ColumnModel.ClientID), columnId); 
        }
    </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 runat="server">
        <title>Ext.Net Example</title>
    
        <script type="text/javascript">
            var setHiddenById = function(colModel, columnId) {
                var index = colModel.getIndexById(columnId);
                colModel.setHidden(index, true);
            }
        </script>
    
    </head>
    <body>
        <form runat="server">
        <ext:ResourceManager runat="server" />
        <ext:GridPanel ID="GridPanel1" runat="server" AutoHeight="true">
            <Store>
                <ext:Store runat="server">
                    <Reader>
                        <ext:ArrayReader>
                            <Fields>
                                <ext:RecordField Name="test1" />
                                <ext:RecordField Name="test2" />
                                <ext:RecordField Name="test3" />
                            </Fields>
                        </ext:ArrayReader>
                    </Reader>
                </ext:Store>
            </Store>
            <ColumnModel runat="server">
                <Columns>
                    <ext:Column ColumnID="Column1" Header="Test1" DataIndex="test1" />
                    <ext:Column ColumnID="Column2" Header="Test2" DataIndex="test2" />
                    <ext:Column ColumnID="Column3" Header="Test3" DataIndex="test3" />
                </Columns>
            </ColumnModel>
        </ext:GridPanel>
        <ext:ComboBox ID="ComboBox1" runat="server" DisplayField="value">
            <Items>
                <ext:ListItem Value="Column1" />
                <ext:ListItem Value="Column2" />
                <ext:ListItem Value="Column3" />
            </Items>
            <SelectedItem Value="Column1" />
        </ext:ComboBox>
        <ext:Button runat="server" Text="SetHiddenById">
            <DirectEvents>
                <Click OnEvent="SetHiddenById">
                    <ExtraParams>
                        <ext:Parameter Name="columnId" Value="ComboBox1.getValue()" Mode="Raw" />
                    </ExtraParams>
                </Click>
            </DirectEvents>
        </ext:Button>
        </form>
    </body>
    </html>
  3. #3
    Quote Originally Posted by csharpdev View Post
    whats the best way of looping over the set of columns on runtime?
    If i use this code:

    foreach (var col in GridPanel1.ColumnModel.Columns)
    {
        if (col.ColumnID.Equals("ID_XYZ"))
        {
    I receive this Exception: Unable to cast object of type 'Ext.Net.CheckColumn' to type 'Ext.Net.Column'.
    Could you provide a sample to reproduce?

    I was unable to reproduce using the code below.

    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                Store store = this.GridPanel1.GetStore();
                store.DataSource = new object[] 
                { 
                    new object[] { "test11", "test12", "test13" },
                    new object[] { "test12", "test22", "test23" },
                    new object[] { "test13", "test32", "test33" }
                };
                store.DataBind();
            }
        }
    
        protected void SetHiddenById(object sender, DirectEventArgs e)
        {
            string columnId = e.ExtraParams["columnId"];
            int index = 0;
            foreach (var col in this.GridPanel1.ColumnModel.Columns)
            {
                if (col.ColumnID.Equals(columnId))
                {
                    this.GridPanel1.ColumnModel.SetHidden(index, true);  
                }
                index++;
            }
        }
    </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 runat="server">
        <title>Ext.Net Example</title>
    </head>
    <body>
        <form runat="server">
        <ext:ResourceManager runat="server" />
        <ext:GridPanel ID="GridPanel1" runat="server" AutoHeight="true">
            <Store>
                <ext:Store runat="server">
                    <Reader>
                        <ext:ArrayReader>
                            <Fields>
                                <ext:RecordField Name="test1" />
                                <ext:RecordField Name="test2" />
                                <ext:RecordField Name="test3" />
                            </Fields>
                        </ext:ArrayReader>
                    </Reader>
                </ext:Store>
            </Store>
            <ColumnModel runat="server">
                <Columns>
                    <ext:CheckColumn />
                    <ext:Column ColumnID="Column1" Header="Test1" DataIndex="test1" />
                    <ext:Column ColumnID="Column2" Header="Test2" DataIndex="test2" />
                    <ext:Column ColumnID="Column3" Header="Test3" DataIndex="test3" />
                </Columns>
            </ColumnModel>
        </ext:GridPanel>
        <ext:ComboBox ID="ComboBox1" runat="server" DisplayField="value">
            <Items>
                <ext:ListItem Value="Column1" />
                <ext:ListItem Value="Column2" />
                <ext:ListItem Value="Column3" />
            </Items>
            <SelectedItem Value="Column1" />
        </ext:ComboBox>
        <ext:Button runat="server" Text="SetHiddenById">
            <DirectEvents>
                <Click OnEvent="SetHiddenById">
                    <ExtraParams>
                        <ext:Parameter Name="columnId" Value="ComboBox1.getValue()" Mode="Raw" />
                    </ExtraParams>
                </Click>
            </DirectEvents>
        </ext:Button>
        </form>
    </body>
    </html>
  4. #4
    hi,

    here the grid, full sample is too big. To have the remove/hide on server side is prefered!

                        <ext:GridPanel ID="GridPanel1" runat="server" Header="false" Height="620px" Border="false"
                            Layout="Fit" TrackMouseOver="true" EnableColumnResize="true" EnableColumnMove="false"
                            EnableColumnHide="false">
                            <Store>
                                <ext:Store runat="server" ID="Store1" OnRefreshData="Store_RefreshData">
                                    <Reader>
                                        <ext:JsonReader IDProperty="PK">
                                            <Fields>
                                                <ext:RecordField Name="PK" />
                                                <ext:RecordField Name="ZZMPGROUP" />
                                                <ext:RecordField Name="MPGROUP_TEXT" />
                                                <ext:RecordField Name="MATNR" />
                                                <ext:RecordField Name="MAKTX" />
                                                <ext:RecordField Name="COLOR" />
                                                <ext:RecordField Name="FASHION_GRADE" />
                                                <ext:RecordField Name="ZZPRICECATEGORY" />
                                                <ext:RecordField Name="PTYPEACT" />
                                                <ext:RecordField Name="KEY_LOOK" />
                                                <ext:RecordField Name="PRINTCAMPAIGN" />
                                                <ext:RecordField Name="KBETR" />
                                                <ext:RecordField Name="GLOBBETR" />
                                                <ext:RecordField Name="GLOBCURCY" />
                                                <ext:RecordField Name="TEXT" />
                                                <ext:RecordField Name="SizeTypeName" />
                                                <ext:RecordField Name="SORT_KEY" />
                                                <ext:RecordField Name="SALESPRODHGRP" />
                                                <ext:RecordField Name="BEZTEXT" />
                                                <ext:RecordField Name="CURCY" />
                                                <ext:RecordField Name="J_3ATQTY" />
                                                <ext:RecordField Name="ID_ORG" />
                                                <ext:RecordField Name="ID_SRT_AREA_ORG" />
                                                <ext:RecordField Name="ZZFORM" />
                                                <ext:RecordField Name="ZZQUALI" />
                                            </Fields>
                                        </ext:JsonReader>
                                    </Reader>
                                    <AutoLoadParams>
                                        <ext:Parameter Name="start" Value="0" Mode="Raw" />
                                        <ext:Parameter Name="limit" Value="4000" Mode="Raw" />
                                    </AutoLoadParams>
                                    <Proxy>
                                        <ext:PageProxy>
                                        </ext:PageProxy>
                                    </Proxy>
                                </ext:Store>
                            </Store>
                            <LoadMask ShowMask="true" />
                            <View>
                                <ext:GridView>
                                    <GetRowClass Fn="markDisabledRecords" />
                                </ext:GridView>
                            </View>
                            <ColumnModel ID="ColumnModel1" runat="server">
                                <Columns>
                                    <ext:Column ColumnID="PICTURE1" Header="_Image" Sortable="false" DataIndex="MATNR"
                                        Width="50px" Resizable="false" Locked="true" Groupable="false" MenuDisabled="true"
                                        Fixed="true" Editable="false">
                                        <Renderer Fn="RenderImage" />
                                    </ext:Column>
                                    <ext:Column ColumnID="MOD_SIZE_ID" Header="_AreaSize" Sortable="false" DataIndex="SizeTypeName"
                                        Align="center" Width="45px" Locked="true">
                                        <Renderer Fn="setGroupColor" />
                                    </ext:Column>
                                    <ext:Column ColumnID="ZZMPGROUP" Header="_CaptionMPG" Sortable="false" DataIndex="ZZMPGROUP"
                                        Align="left" Width="80px" Locked="true">
                                        <Renderer Fn="RenderGroup" />
                                    </ext:Column>
                                    <ext:Column ColumnID="SALESPRODHGRP" Header="_CaptionSalesPHG" Sortable="false" DataIndex="SALESPRODHGRP"
                                        Align="left" Width="70px" Locked="true">
                                        <Renderer Fn="RenderSalesProdGrp" />
                                    </ext:Column>
                                    <ext:Column ColumnID="MATNR" Header="Style" Sortable="false" DataIndex="MATNR" Width="60px"
                                        Align="Center">
                                        <Renderer Fn="RenderStyle" />
                                    </ext:Column>
                                    <ext:Column ColumnID="MAKTX" Header="_CaptionMAKTX" Sortable="false" DataIndex="MAKTX"
                                        Width="155px" Align="Left">
                                        <Renderer Fn="RenderMAKTX" />
                                    </ext:Column>
                                    <%--
                                    <ext:Column ColumnID="ZZFORM" Header="_CaptionZZFORM" Sortable="false" DataIndex="ZZFORM"
                                        Align="Right" Width="60px" Locked="true">
                                        <Renderer Fn="RenderInt" />
                                    </ext:Column>
                                    --%>
                                    <ext:Column ColumnID="ZZQUALI" Header="_CaptionZZQUALI" Sortable="false" DataIndex="ZZQUALI"
                                        Align="Right" Width="70px" Locked="true">
                                        <Renderer Fn="RenderInt" />
                                    </ext:Column>
                                    <ext:Column ColumnID="COLOR" Header="_CaptionColor" Sortable="false" DataIndex="COLOR"
                                        Align="center" Width="40px" Locked="true">
                                    </ext:Column>
                                    <ext:Column ColumnID="FASHION_GRADE" Header="_CaptionFashionGrade" Sortable="false"
                                        DataIndex="FASHION_GRADE" Align="center" Width="80px" Locked="true">
                                        <Renderer Fn="RenderFashionGrade" />
                                    </ext:Column>
                                    <ext:Column ColumnID="GLOBBETR" Header="_CaptionRTL_C_ZZ" Sortable="false" DataIndex="GLOBBETR"
                                        Align="Right" Width="85px" Locked="true">
                                        <Renderer Fn="RenderMoneyGlob" />
                                    </ext:Column>
                                    <ext:Column ColumnID="KBETR" Header="_CaptionCountryPrice" Sortable="false" DataIndex="KBETR"
                                        Align="Right" Width="85px" Locked="true">
                                        <Renderer Fn="RenderMoney" />
                                    </ext:Column>
                                    <ext:Column ColumnID="J_3ATQTY" Header="_CaptionQTY" Sortable="false" DataIndex="J_3ATQTY"
                                        Align="Center" Width="40px" Locked="true">
                                    </ext:Column>
                                    <ext:Column ColumnID="CalcSum" Header="_CaptionQTYSum" Sortable="false" DataIndex="PK"
                                        Align="Right" Width="100px" Locked="true">
                                        <Renderer Fn="RenderSum" />
                                    </ext:Column>
                                    <ext:Column ColumnID="KEY_LOOK" Header="_CaptionKeyLook" Sortable="false" DataIndex="KEY_LOOK"
                                        Align="Center" Width="55px">
                                        <Renderer Fn="RenderKeylook" />
                                    </ext:Column>
                                    <ext:CheckColumn ColumnID="chk_PRINTCAMPAIGN" Header="_CaptionPrintCamp" Sortable="false"
                                        DataIndex="PRINTCAMPAIGN" Align="Center" Width="55px" Locked="true">
                                        <Renderer Fn="RenderPrintCampaign" />
                                    </ext:CheckColumn>
                                    <ext:Column ColumnID="TEXT" Header="_CaptionMaterial" Sortable="false" DataIndex="TEXT"
                                        Align="Right" Width="150px" Locked="true">
                                        <Renderer Fn="RenderWithTooltip" />
                                    </ext:Column>
                                    <ext:Column ColumnID="ID_ORG" Header="_CountryAdaption" Sortable="false" DataIndex="ID_ORG"
                                        Align="Center" Width="55px" Locked="true">
                                        <Renderer Fn="RenderAdaption" />
                                    </ext:Column>
                                    <ext:ImageCommandColumn Width="25" Align="Right" Locked="true">
                                        <Commands>
                                            <ext:ImageCommand CommandName="Delete" Icon="Delete">
                                                <ToolTip Text="Delete" />
                                            </ext:ImageCommand>
                                        </Commands>
                                        <PrepareCommand Fn="prepareCommand" />
                                    </ext:ImageCommandColumn>
                                </Columns>
                                <%--
                                    <ext:Column ColumnID="ZZPRICECATEGORY" Header="_CaptionPriceLevel" Sortable="false"
                                        DataIndex="ZZPRICECATEGORY" Align="center" Width="60px" Locked="true">
                                        <Renderer Fn="RenderPricecategory" />
                                    </ext:Column>
                                     %><%--
                                    <ext:Column ColumnID="PTYPEACT" Header="_CaptionPTypEact" Sortable="false" DataIndex="PTYPEACT"
                                        Align="center" Width="70px">
                                        <Renderer Fn="RenderPTypeact" />
                                    </ext:Column>--%>
                            </ColumnModel>
                            <Listeners>
                                <Command Handler="if(command == 'Delete'){deleteRow(record)};" />
                                <CellClick Fn="CellClick" />
                            </Listeners>
                            <Plugins>
                                <ext:GridFilters runat="server" ID="GridFilters1" Local="true">
                                    <Filters>
                                        <ext:StringFilter DataIndex="MATNR" />
                                        <ext:StringFilter DataIndex="COLOR" />
                                        <ext:StringFilter DataIndex="ZZFORM" />
                                        <ext:StringFilter DataIndex="MAKTX" />
                                        <ext:StringFilter DataIndex="ZZQUALI" />
                                        <ext:ListFilter DataIndex="KEY_LOOK" Options="X" />
                                        <ext:ListFilter DataIndex="PRINTCAMPAIGN" Options="X" />
                                        <ext:NumericFilter DataIndex="KBETR" />
                                        <ext:NumericFilter DataIndex="GLOBBETR" />
                                        <ext:NumericFilter DataIndex="J_3ATQTY" />
                                        <ext:StringFilter DataIndex="TEXT" />
                                    </Filters>
                                </ext:GridFilters>
                            </Plugins>
                            <BottomBar>
                                <ext:PagingToolbar Height="1px" ID="PagingToolBar2" runat="server" StoreID="Store1"
                                    PageSize="5000" DisplayInfo="true" HideRefresh="true" DisplayMsg="" Visible="false">
                                </ext:PagingToolbar>
                            </BottomBar>
                            <SelectionModel>
                                <ext:RowSelectionModel ID="RowSelectionModel1" runat="server" SingleSelect="true">
                                </ext:RowSelectionModel>
                            </SelectionModel>
                            <KeyMap>
                                <ext:KeyBinding>
                                    <Keys>
                                        <ext:Key Code="DELETE" />
                                    </Keys>
                                    <Listeners>
                                        <Event Fn="deleteRows" />
                                    </Listeners>
                                </ext:KeyBinding>
                            </KeyMap>
                        </ext:GridPanel>
  5. #5
    To have the remove/hide on server side is prefered!
    Don't you like a solution with X.Call()? Well, there is no better way to achieve this requirement. Please note that .SetHidden() method also produces client-side script. But this is just wrapped in server-side method.

    Well, you could create your own custom control inherits from Ext.Net.ColumnModel with .SetHiddenById() server-side method.

    I receive this Exception: Unable to cast object of type 'Ext.Net.CheckColumn' to type 'Ext.Net.Column'.
    I have looked at your code and I still cannot reproduce this exception. Could you provide a full sample to reproduce? You could use the sample that I posted as a base.
  6. #6
    Hi,

    I cleaned and recompiled the whole Ext.Net SVN and now I works like a charme,
    this solved the cast exception. This is now working:

        foreach (var col in GridPanel1.ColumnModel.Columns)
        {
            if (col.ColumnID.Equals("COLOR"))
            {
                col.Hidden = true;
            }
        }
    I needed by ID because the order can dynamically change depeding on datasource.
    Something like this would be more elegant

    this.GridPanel1.ColumnModel.SetHidden("ColumnID", true); 
    this.GridPanel1.ColumnModel.Columns["ColumnID"].Hidden = true;
    but I'm fine with this method in base page abstractions:

            private void hideColumn(string id, GridPanel grid)
            {
                foreach (var col in grid.ColumnModel.Columns)
                {
                    if (col.ColumnID.Equals(id))
                    {
                        col.Hidden = true;
                    }
                }
            }
    usage
    hideColumn("COLOR", GridPanel1);
    Pls, mark as solved.
  7. #7
    Thanks for the update.
  8. #8

    Create a new method and get the index

    I tried the above solutions but none worked.

    I decided to right a method that gets the array index of the column in the grid and then use the standard SetHidden method.

      //get the index of the column in the grid
      var columnIndex = getColumnIndex("columnName", dataGrid);
    
      //hide the column
      this.dataGrid.ColumnModel.SetHidden(columnIndex, false);
    
    
      private int getColumnIndex(string id, GridPanel grid)
        {
            int ctr = 0;
            foreach (var col in grid.ColumnModel.Columns)
            { 
                if (col.ColumnID.Equals(id))
                {
                    return ctr;
                }
                ctr++;
            }
            return ctr;
        }
    Last edited by fabricio.murta; Jun 16, 2017 at 11:54 PM. Reason: Wrap code in [code][/code] tags.

Similar Threads

  1. [CLOSED] [Razor] GridPanel ColumnModel Commands
    By Timothy in forum 2.x Premium Help
    Replies: 1
    Last Post: May 09, 2012, 9:39 PM
  2. Replies: 2
    Last Post: Oct 07, 2011, 3:55 PM
  3. Replies: 2
    Last Post: Oct 07, 2011, 3:54 PM
  4. Selecting different columnmodel for gridpanel.
    By masudcseku in forum 1.x Help
    Replies: 5
    Last Post: May 11, 2011, 5:09 PM
  5. Add ColumnModel to GridPanel
    By aalkema in forum 1.x Help
    Replies: 0
    Last Post: Apr 27, 2009, 11:58 AM

Tags for this Thread

Posting Permissions

Subscribe now to get the latest Ext.NET happenings in our newsletter.