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

SelectedIndexChanged event of RadGrid

11 Answers 1136 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
zpc
Top achievements
Rank 1
zpc asked on 11 Sep 2011, 01:42 PM
Hi,

SelectedIndexChanged event of RadGrid is not fired.
Please help me with an example how to fire SelectedIndexChanged event of RadGrid.

11 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 12 Sep 2011, 05:26 AM
Hello,

The SelectedIndexChanged event will be fired when you click the row if you have set EnablePostBackOnRowClick property to True.
aspx:
<ClientSettings Selecting-AllowRowSelect="true" EnablePostBackOnRowClick="true">
</ClientSettings>
C#:
protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
{
   GridDataItem item =(GridDataItem )RadGrid1.SelectedItems[0];//get selected row
}

Thanks,
Shinu.
0
Michael
Top achievements
Rank 1
answered on 20 Mar 2014, 04:21 PM
How would you fire the Selected Index Change (i.e. the user clicks on a Row in the RadGrid) without using a PostBack?  The PostBack is not acceptable in my situation, therefore this method is not going to work for me because it triggers the PostBack on the page.

I need to listen to this event without a PostBack being called.
0
Shinu
Top achievements
Rank 2
answered on 21 Mar 2014, 04:15 AM
Hi Michael,

Please try to ajaxify the RadGrid to achieve your scenario.

ASPX:
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadGrid1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1"                                                           AutoGenerateColumns="true" OnSelectedIndexChanged="RadGrid1_SelectedIndexChanged">
    <ClientSettings Selecting-AllowRowSelect="true" EnablePostBackOnRowClick="true">
    </ClientSettings>
</telerik:RadGrid>

C#:
protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridDataItem item = (GridDataItem)RadGrid1.SelectedItems[0];//get selected row
}

Thanks,
Shinu.
0
Michael
Top achievements
Rank 1
answered on 21 Mar 2014, 02:12 PM
The post back is still being fired (I know because the Loading Panel overlay appears).

<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" HorizontalAlign="NotSet" LoadingPanelID="RadAjaxLoadingPanel2">
   <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" 
            CellSpacing="0" OnNeedDataSource="RadGrid1_NeedDataSource" CssClass="RemoveBorders" 
            OnItemDataBound="RadGrid1_ItemDataBound" OnPreRender="RadGrid1_PreRender" 
            GridLines="None" Width="770px"  OnSelectedIndexChanged="RadGrid1_SelectedIndexChanged">
            <ClientSettings EnablePostBackOnRowClick="true">
                <Selecting AllowRowSelect="true"></Selecting>
            </ClientSettings>
            <MasterTableView>
                <Columns>






0
Princy
Top achievements
Rank 2
answered on 25 Mar 2014, 09:31 AM
Hi Michael,

Unfortunately I couldn't replicate the issue at my end. Please have a look into the sample code snippet which works fine at my end.

ASPX:
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default">
</telerik:RadAjaxLoadingPanel>
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" LoadingPanelID="RadAjaxLoadingPanel1">
    <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1"                          AutoGenerateColumns="true" OnSelectedIndexChanged="RadGrid1_SelectedIndexChanged">
        <ClientSettings Selecting-AllowRowSelect="true" EnablePostBackOnRowClick="true">
        </ClientSettings>
    </telerik:RadGrid>
</telerik:RadAjaxPanel>
protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
{
    Thread.Sleep(1000);
    //your code
}

Please try the posted code and let me know it is working or not. Please provide your full code if it doesn't help.
Thanks,
Princy.
0
Hori
Top achievements
Rank 1
answered on 02 May 2016, 09:31 AM

Im always getting an error of System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. in the line of 

      GridDataItem selectedItem = (GridDataItem)vListRadGridChk.SelectedItems[0];

0
Viktor Tachev
Telerik team
answered on 04 May 2016, 11:54 AM
Hi Hori,

The error you are seeing could be observed if an item is not selected in the grid. However, it would be hard to pinpoint what is causing it without further investigation.

Would you send us a runnable sample where the issue you are seeing us replicated? Alternatively you can provide the relevant markup with the code-behind. Thus we can examine the code and look for what could cause the behavior.

Regards,
Viktor Tachev
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Animesh
Top achievements
Rank 1
answered on 22 Feb 2018, 05:49 PM

 

Hi I am also getting error of out of range exception just like Hori. 
<telerik:RadGrid ID="TGRCircularAIBOC" runat="server" Skin="Office2007" OnSelectedIndexChanged="TGRCircularAIBOC_SelectedIndexChanged">  
                        <ClientSettings Selecting-AllowRowSelect="true" EnablePostBackOnRowClick="true" ></ClientSettings>
                    </telerik:RadGrid>
 
GridDataItem dataitem = (GridDataItem)TGRCircularAIBOC.SelectedItems[1];
        string CircularNo = dataitem["CircularNo"].ToString();
0
Attila Antal
Telerik team
answered on 23 Feb 2018, 09:10 AM
Hi Animesh,

Try accessing the item with the index of zero (0)
GridDataItem dataitem = (GridDataItem)TGRCircularAIBOC.SelectedItems[0];

Out of range exception usually occurs when trying to access an item with an index that does not exist in the SelectedItems collection.

You could, of course, have multiple selected rows if you enable the AllowMultiRowSelection property of RadGrid then try accessing the items using their index. Let's say, you could loop through the items like:
protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
{
    for (int i = 0; i < TGRCircularAIBOC.SelectedItems.Count; i++)
    {
        GridDataItem dataitem = (GridDataItem)TGRCircularAIBOC.SelectedItems[i];
        // do something
    }
}

I've attached a basic sample for demonstration.

I hope this helps.

Kind Regards,
Attila Antal
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Animesh
Top achievements
Rank 1
answered on 25 Feb 2018, 06:10 PM

Hi Attila Antal

Thnx for ur reply..basic prob is solved. If you go through my selectedindexchanged event I am trying to read a varbinary data from SQL and open it. I want to open it in a new window or tab but that is not happening and secondly after executing binary write I am still in the selectedindexchanged event, how to get rid of it. I am giving you both aspx and cs file

<%@ Page Title="" Language="C#" MasterPageFile="~/BOBEA.master" AutoEventWireup="true"
    CodeFile="Circular.aspx.cs" Inherits="_Circular" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
    <telerik:RadScriptManager ID="RSMCircular" runat="server">
        <Scripts>
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
        </Scripts>
    </telerik:RadScriptManager>
    <telerik:RadAjaxManager ID="RAMCircular" runat="server">
    </telerik:RadAjaxManager>
    <div class="Information">
        <fieldset class="login">
            <legend>Circulars</legend>
            <table class="table">
                <tr>
                    <td>
                        <asp:Label ID="lblAuthorityCode" runat="server" Text="Authority Code"></asp:Label>
                    </td>
                    <td>
                        <telerik:RadComboBox ID="RCBAuthorityCode" runat="server" DataTextField="AuthorityCode" DataValueField="AuthorityCode" MarkFirstMatch="true" Skin="Office2007">
                            <Items>
                                <telerik:RadComboBoxItem runat="server" Text="AIBEA" Value="AIBEA" />
                                <telerik:RadComboBoxItem runat="server" Text="AIBOC" Value="AIBOC" />
                                <telerik:RadComboBoxItem runat="server" Text="BEFI" Value="BEFI" />
                                <telerik:RadComboBoxItem runat="server" Text="BOBEA" Value="BOBEA" />
                                <telerik:RadComboBoxItem runat="server" Text="BOBEU" Value="BOBEU" />
                                <telerik:RadComboBoxItem runat="server" Text="BOBKS" Value="BOBKS" />
                                <telerik:RadComboBoxItem runat="server" Text="INBOC" Value="INBOC" />
                                <telerik:RadComboBoxItem runat="server" Text="NCBE" Value="NCBE" />
                            </Items>
                        </telerik:RadComboBox>
                    </td>
                    <td>
                        <telerik:RadButton ID="RBCircular" runat="server" Text="Circular" OnClick="RBCircular_Click" Skin="Office2007"></telerik:RadButton>
                    </td>
                </tr>
            </table>
            <telerik:RadGrid ID="TGRCircular" runat="server" Skin="Office2007" OnNeedDataSource="TGRCircular_NeedDataSource"
                OnSelectedIndexChanged="TGRCircular_SelectedIndexChanged" AllowPaging="true" AutoGenerateColumns="true">
                <ClientSettings Selecting-AllowRowSelect="true" EnablePostBackOnRowClick="true"></ClientSettings>
            </telerik:RadGrid>
        </fieldset>
    </div>
</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web;
using Telerik.Web.UI;
 
public partial class _Circular : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    protected void RBCircular_Click(object sender, EventArgs e)
    {
        string AuthorityCode = RCBAuthorityCode.Text.ToString();
        TGRCircular.DataSource = Resources.GetCircular(AuthorityCode);
        TGRCircular.DataBind();
    }
 
    protected void TGRCircular_SelectedIndexChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < TGRCircular.SelectedItems.Count; i++)
        {
            GridDataItem dataitem = (GridDataItem)TGRCircular.SelectedItems[0];
            string CircularNo = dataitem["CircularNo"].Text.ToString();
            string AuthorityCode = dataitem["AuthorityCode"].Text.ToString();
            byte[] BinaryPDF = Resources.ViewCircular(CircularNo, AuthorityCode);
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment; filename = " + "Circular");
            Response.BufferOutput = true;
            Response.BinaryWrite(BinaryPDF);
            //Response.Write(string.Format("<script>window.open('%temp%','_blank');</script>"));
            //Response.Write("<script>");
            //Response.Write("window.open('%temp%', '_newtab');");
            //Response.Write("</script>");
            TGRCircular.DataSource = Resources.GetCircular(AuthorityCode);
            TGRCircular.DataBind();
            Response.Flush();
            Response.End();
 
 
        }
    }
 
    protected void TGRCircular_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        string AuthorityCode = RCBAuthorityCode.Text.ToString();
        TGRCircular.DataSource = Resources.GetCircular(AuthorityCode);
    }
}
0
Attila Antal
Telerik team
answered on 27 Feb 2018, 04:01 PM
Hi Animesh,

Calling DataBind() method when using Advanced DataBinding with NeedDataSource is not supported. Instead, we recommend using Rebind().

You can implement something similar to the example below:
public string AuthorityCode { get; set; }
 
protected void TGRCircular_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    AuthorityCode = RCBAuthorityCode.Text.ToString(); // set the value for AuthorityCode
    TGRCircular.DataSource = Resources.GetCircular(AuthorityCode); // bind the data to RadGrid
}
 
protected void RBCircular_Click(object sender, EventArgs e)
{
    AuthorityCode = RCBAuthorityCode.Text.ToString(); // Set new value for AuthorityCode
    TGRCircular.Rebind(); // Make RadGrid rebind the data filtered by the new values
}
 
protected void TGRCircular_SelectedIndexChanged(object sender, EventArgs e)
{
    AuthorityCode = "NewValue"; // Set new value for AuthorityCode
    TGRCircular.Rebind(); // Make RadGrid rebind the data filtered by the new values
}

Please try the suggestion from above to see if that works for you.

Kind regards,
Attila Antal
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
General Discussions
Asked by
zpc
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Michael
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Hori
Top achievements
Rank 1
Viktor Tachev
Telerik team
Animesh
Top achievements
Rank 1
Attila Antal
Telerik team
Share this question
or