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

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

5 Answers 973 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Simon
Top achievements
Rank 1
Simon asked on 20 Aug 2014, 07:21 AM
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!

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 20 Aug 2014, 08:22 AM
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
0
Simon
Top achievements
Rank 1
answered on 21 Aug 2014, 02:47 AM
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
0
Eyup
Telerik team
answered on 22 Aug 2014, 08:11 AM
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.

 
0
Simon
Top achievements
Rank 1
answered on 03 Sep 2014, 03:14 AM
Hi Eyup,

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


0
Srividhya
Top achievements
Rank 1
answered on 02 Aug 2016, 01:37 PM

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

 

Tags
Grid
Asked by
Simon
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Simon
Top achievements
Rank 1
Eyup
Telerik team
Srividhya
Top achievements
Rank 1
Share this question
or