How select first row of a grid with also the event od selected row

1 Answer 87 Views
Grid
RRE
Top achievements
Rank 2
Iron
RRE asked on 22 Apr 2023, 05:45 AM

I would like to select and fire the event of the first row of a grid on loading time, server side.

How to select a row is easy 

RagGrid1.Items[0].Selected = true;

but this doesn't fire the event

OnItemCommand="..."

 

any idea ?

thanks

Renato

1 Answer, 1 is accepted

Sort by
0
Accepted
Attila Antal
Telerik team
answered on 26 Apr 2023, 05:09 PM

Hi Renato,

The ItemCommand is an event that is triggered only when the user interacts with it. Only setting a row's Selected property, will not trigger the event. You can, however, programmatically invoke the ItemCommand.

  • Items are not available at page load, so selecting them can be done by adding the item index to the selected index collection.
  • Accessing the item to trigger a command event, can be done in the PreRender event.
  • Capturing the triggered command can be done in the ItemCommand event.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // Select an item by adding the itemindex to the SelectedIndexes collection in the PageLoad
        RadGrid1.SelectedIndexes.Add(new int[] { 0 });
    }
}

protected void Page_PreRender(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // access the first item (NOT available in Page Load, that is why using the PreRender)
        GridItem firstItem = RadGrid1.Items[0];
        // trigger a Custom Command event with params
        firstItem.FireCommandEvent("MyCustomCommand", "SomeArgumentsHere");
    }
}

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if(e.CommandName == "MyCustomCommand")
    {
        string arguments = e.CommandArgument.ToString(); //returns: SomeArgumentsHere
    }
}

 

For more details, please refer to the following articles:

 

Regards,
Attila Antal
Progress Telerik

Heads up! Telerik UI for ASP.NET AJAX versions for .NET 3.5 and 4.0 are retired. Progress will continue shipping assemblies compatible with .NET 4.5 and later. See whether this affects your apps in this article.
RRE
Top achievements
Rank 2
Iron
commented on 27 Apr 2023, 07:03 AM | edited

Thank you, Attila,

very interesting!

Renato

Tags
Grid
Asked by
RRE
Top achievements
Rank 2
Iron
Answers by
Attila Antal
Telerik team
Share this question
or