Get selected row

2 posts, 0 answers
  1. Justin
    Justin avatar
    1 posts
    Member since:
    Oct 2013

    Posted 16 Oct 2013 Link to this post

    I need to be able to get the selected row that contains a combobox when the combobox selectedindex changes.  I have a radgrid that contains a combobox control in one of the columns.  At runtime the combo box is populated and set to the proper selection during the itemdatabound method.  The user can then change the selected item in the combobox which I need to grab other cell values in that same grid row.  All this needs to be done in javascript.  I have found articles where I need to get combobox.parent.parent to get the selected index row which I have been able to achive by doing 

    var parent = sender.get_parent;
    var grandparent = parent.get_parent;

    But have not been able to successfully get the selected row to be able to get values from other cells in that row.  Any suggestions on how to get the information out using javascript would be appreciated

    Thanks
    Justin
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 18 Oct 2013 Link to this post

    Hi Justin,

    I guess you need to access the row on RadComboBox "OnClientSelectedIndexChanged" event. Please take a look into the following code snippet I tried.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="RadGrid1_ItemDataBound"
            OnItemCreated="RadGrid1_ItemCreated" AutoGenerateColumns="false">
            <MasterTableView>
                <Columns>
                    <telerik:GridBoundColumn DataField="OrderID" UniqueName="OrderID" HeaderText="OrderID">
                    </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn HeaderText="Combobox">
                        <ItemTemplate>
                            <telerik:RadComboBox ID="RadComboBox1" runat="server">
                            </telerik:RadComboBox>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
            <ClientSettings Selecting-AllowRowSelect="true">
            </ClientSettings>
        </telerik:RadGrid>

    C#:
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            RadComboBox combo = (RadComboBox)item.FindControl("RadComboBox1");
            combo.Items.Insert(0, new RadComboBoxItem("item0", "0"));
            combo.Items.Insert(1, new RadComboBoxItem("item1", "1"));
            combo.Items.Insert(2, new RadComboBoxItem("item2", "2"));
            combo.Items.Insert(3, new RadComboBoxItem("item3", "3"));
            combo.Items.Insert(4, new RadComboBoxItem("item4", "4"));
            combo.Items.Insert(5, new RadComboBoxItem("item5", "5"));
            combo.SelectedIndex=3;
        }
    }
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
              
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            int index = item.ItemIndex;
            RadComboBox combo = (RadComboBox)item.FindControl("RadComboBox1");
            combo.OnClientSelectedIndexChanged = "function(sender, args){OnClientSelectedIndexChanged1(sender, args,'"+index+"' );}";
        }
    }

    JavaScript:
    <script type="text/javascript">
        function OnClientSelectedIndexChanged1(sender, args, index) {
            var grid = $find("<%=RadGrid1.ClientID %>");
            var mastertable = grid.get_masterTableView();
            var dataitem = mastertable.get_dataItems()[index]; //accessing the row in which the RadComboBox selectedIndexChanged event is invoked
            alert(dataitem.get_cell("OrderID").innerHTML); //getting the cell value of the row
        }
    </script>

    Thanks,
    Princy.
Back to Top