Get the row index at SelectedIndexChanged event handler of RadDropDownList in GridTemplateColumn

6 posts, 0 answers
  1. Simon
    Simon avatar
    5 posts
    Member since:
    Jul 2014

    Posted 20 Aug 2014 Link to this post

    Hi,

    I am using RadDropDownList in GridTemplateColumn, and I want to get the row index when SelectedIndexChanged event of RadDropDownList is fired.
    My code looks like below:

    ASPX:
            <telerik:RadGrid runat="server" ID="RadGrid1" AllowSorting="false" AutoGenerateColumns="false" AllowMultiRowSelection="True" GridLines="Both">
                <ClientSettings AllowColumnsReorder="false" ReorderColumnsOnClient="False">
                    <Scrolling AllowScroll="true" UseStaticHeaders="true"></Scrolling>
                    <Resizing AllowColumnResize="true" EnableRealTimeResize="false" />
                    <Selecting AllowRowSelect="True"></Selecting>
                </ClientSettings>
                <GroupingSettings ShowUnGroupButton="False"></GroupingSettings>
     
                <MasterTableView EnableHeaderContextMenu="false" EditMode="Batch" >
                    <BatchEditingSettings EditType="Cell" OpenEditingEvent="MouseOver"/>
                    <Columns>
                        <telerik:GridClientSelectColumn Reorderable="False" Resizable="False" >
                        </telerik:GridClientSelectColumn>
                        <telerik:GridBoundColumn DataField="Organ" HeaderText="Organ" Resizable="False" DataType="System.String" ReadOnly="true">
                        </telerik:GridBoundColumn>
                        <telerik:GridTemplateColumn UniqueName="OptionColumn" HeaderText="Option" Resizable="False">
                            <ItemTemplate >
                                <%# Eval("Option")%>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <telerik:RadDropDownList ID="MyList" runat="server" DataSourceID="LinqDS1" DataTextField="Text" DataValueField="Value" OnSelectedIndexChanged="MyList_SelectedIndexChanged" AutoPostBack="True">
                                </telerik:RadDropDownList>
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>
     
    <asp:LinqDataSource ID="LinqDS1" runat="server" EnableDelete="False" EnableInsert="False" EnableUpdate="false" OnSelecting="LinqDS1_Selecting"></asp:LinqDataSource>

    Code-behind:
    01.protected void MyList_SelectedIndexChanged(object sender, DropDownListEventArgs e)
    02.{
    03.    var dropDownList = sender as RadDropDownList;
    04.    if (dropDownList != null)
    05.    {
    06.        // try to get the row index
    07.        var item = dropDownList.NamingContainer as GridDataItem;
    08.        if( item != null )
    09.        {
    10.            sources[ item.RowIndex ] = e.Text;
    11.        }
    12.    }
    13.}

    I searched in the forum and find some hints as above code snippet, but at line 7, I always get a null reference.

    Do you have any advice? Thanks in advance!
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 20 Aug 2014 in reply to Simon Link to this post

    Hi Simon,

    Please try the following code snippet to get the Index of the edited row.

    C#:
    protected void MyList_SelectedIndexChanged(object sender, DropDownListEventArgs e)
    {
      RadDropDownList dropDownList = (RadDropDownList)sender;
      GridEditableItem editItem = (GridEditableItem)dropDownList.NamingContainer;      
      int index = editItem.ItemIndex;//get index
    }

    Thanks,
    Princy
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Simon
    Simon avatar
    5 posts
    Member since:
    Jul 2014

    Posted 20 Aug 2014 in reply to Princy Link to this post

    Hi Princy,

    Thanks for your prompt reply!

    I tried with your solution and got below exception, the dropDownList.NamingContainer cannot be cast to GridEditableItem object.
    Do you know what the problem is?
    Unable to cast object of type 'PanelNamingContainer' to type 'Telerik.Web.UI.GridEditableItem'.
     
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
     
    Exception Details: System.InvalidCastException: Unable to cast object of type 'PanelNamingContainer' to type 'Telerik.Web.UI.GridEditableItem'.
     
    Source Error:
     
     
    Line 145:        var dropDownList = (RadDropDownList)sender;
    Line 146:
    Line 147:        var editItem = (GridEditableItem)dropDownList.NamingContainer;
    Line 148:
    Line 149:        int index = editItem.ItemIndex;//get index
     
    Source File: d:\Program\testWeb\Default.aspx.cs    Line: 147
  5. Eyup
    Admin
    Eyup avatar
    3015 posts

    Posted 22 Aug 2014 Link to this post

    Hi Simon,

    Please note that when the selected edit mode of the grid is Batch, there are some specifics. You can check the following article to see how to get the corresponding Editor control on the server:
    http://www.telerik.com/help/aspnet-ajax/grid-batch-editing.html

    However, please note that having an edit control with enabled AutoPostBack property will not be convenient with Batch editing since it will erase any other changes made by the user when it posts back to the server. Therefore, I would suggest that in this case you use the client-side SelectedIndexChanged event handler of the dropdownlist.

    Hope this helps.

    Regards,
    Eyup
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  6. Simon
    Simon avatar
    5 posts
    Member since:
    Jul 2014

    Posted 02 Sep 2014 in reply to Eyup Link to this post

    Hi Eyup,

    Thanks for your advice!
    It works if use client side event handler.


  7. Srividhya
    Srividhya avatar
    2 posts
    Member since:
    Jun 2016

    Posted 02 Aug in reply to Simon Link to this post

    Could you please post your solution on this issue, I too facing the same issue. Thank You

     

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017