Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close Telerik Radgrid Editform If Already Opened

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?

like image 318
Lucky Pierre Avatar asked Apr 04 '13 19:04

Lucky Pierre


3 Answers

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();
        }
    }
like image 52
Lucky Pierre Avatar answered Nov 14 '22 21:11

Lucky Pierre


Please try with the below code snippet.

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.EditCommandName)
    {
        RadGrid1.MasterTableView.ClearEditItems();
    }
}
like image 30
Jayesh Goyani Avatar answered Nov 14 '22 21:11

Jayesh Goyani


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
    }
}
like image 2
dst3p Avatar answered Nov 14 '22 23:11

dst3p