Close Telerik Radgrid Editform If Already Opened
Asked Answered
P

3

5

I've spent the past hour or two trying to find a solution to what I think should be easy to do and something that I would think has been asked before, but perhaps I'm not using the correct terms.

I have a very basic RadGrid that allows rows to be expanded for editing or showing more stuff. Here is the GridEditCommandColumn I have inside of

<rad:GridEditCommandColumn EditText="+" UniqueName="EditCommandColumn" ItemStyle-Width="30" HeaderStyle-Width="30" />

Again, nothing special. When the grid loads, there's a "+" character for each row allowing it to expand. If I click on it, it correctly opens as it should. If I click another row, it closes the one I had open and opens the one I clicked on. Great, all is fine.

Now what I've been trying to search for is if I have a row already open and I click the "+" link again, I'd like this row to close if it's already open. Right now, it remains opened.

Am I the only one who's ever wanted it to close if you click it again if it's already open?

Petroglyph answered 4/4, 2013 at 19:40 Comment(0)
F
3

In our application we do something similar, however rather than clicking the plus button again, we click a cancel button. For this cancel button, we don't actually have any specific code, but we set the CommandName="Cancel" in the markup then check for CommandName in the item command event & write our code there. The grid is then bound, and the edit form will be closed. Sorry it doesn't answer your root question, but hopefully something to kick start you.

Below is the sample event structure where you can filter with the CommandName & write your logic.

protected void rg_ItemCommand(object sender, GridCommandEventArgs e)
{            
    if (e.CommandName == "Cancel")
    {
      //custom logic here
    }
}
Fungible answered 4/4, 2013 at 21:42 Comment(1)
I liked your recommendation! It led to me a solution that I posted below. Thanks man!Petroglyph
P
5

Based on a recommendation from dstepan, I was able to solve it. I got rid of the generic GridEditCommandColumn row and replaced it with this.

<rad:GridTemplateColumn UniqueName="ExpandRow">
   <ItemTemplate>
      <asp:Button ID="btnExpand" CommandName="ExpandRow" CommandArgument="<%# Container.ItemIndex %>" Text="+" runat="server" />
    </ItemTemplate>
 </rad:GridTemplateColumn>

And then for the event handler

protected void rg_ItemCommand(object sender, GridCommandEventArgs e)
    {            
        if (e.CommandName == "ExpandRow")
        {
            GridDataItem item = rg.Items[int.Parse(e.CommandArgument.ToString())];

            item.Edit = item.Edit ? false : true; // If it's already in edit mode, change it to false. If not, set it to true

            rg.Rebind();
        }
    }
Petroglyph answered 5/4, 2013 at 13:18 Comment(0)
F
3

In our application we do something similar, however rather than clicking the plus button again, we click a cancel button. For this cancel button, we don't actually have any specific code, but we set the CommandName="Cancel" in the markup then check for CommandName in the item command event & write our code there. The grid is then bound, and the edit form will be closed. Sorry it doesn't answer your root question, but hopefully something to kick start you.

Below is the sample event structure where you can filter with the CommandName & write your logic.

protected void rg_ItemCommand(object sender, GridCommandEventArgs e)
{            
    if (e.CommandName == "Cancel")
    {
      //custom logic here
    }
}
Fungible answered 4/4, 2013 at 21:42 Comment(1)
I liked your recommendation! It led to me a solution that I posted below. Thanks man!Petroglyph
R
3

Please try with the below code snippet.

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.EditCommandName)
    {
        RadGrid1.MasterTableView.ClearEditItems();
    }
}
Rowlandson answered 5/4, 2013 at 6:34 Comment(1)
I've tried something like this, but to no avail. It does go into that if-block, but somewhere after here it must get added back in.Petroglyph

© 2022 - 2024 — McMap. All rights reserved.