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

How to force specific row in grid to be in edit mode after user triggered event

1 Answer 48 Views
Grid
This is a migrated thread and some comments may be shown as answers.
MRa
Top achievements
Rank 1
MRa asked on 09 Aug 2013, 04:19 PM
The idea is the user clicks a button, a new row is added to the DB and the grid is rebind() to include the new added row. Is it possible for the last added item to be shown in edit mode? I'm using InPlace editing.

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 14 Aug 2013, 05:37 AM
Hi,

You can try the below code snippet to achieve this scenario.I guess you are adding and inserting on button click,and want the inserted item to be in edit mode.Please try the code and let me know if any concern.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"    OnInsertCommand="RadGrid1_InsertCommand" AutoGenerateEditColumn="true" OnPreRender="RadGrid1_PreRender" OnItemCommand="RadGrid1_ItemCommand">
    <MasterTableView DataKeyNames="OrderID" CommandItemDisplay="Top" EditMode="InPlace">     
        <Columns>
            <telerik:GridBoundColumn HeaderText="OrderID" DataField="OrderID" UniqueName="OrderID" />
            <telerik:GridBoundColumn HeaderText="ShipName" DataField="ShipName" UniqueName="ShipName" />       
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
<asp:Button ID="Button1" runat="server" Text="AddNew" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Insert" OnClick="Button2_Click" />

C#:
 bool isItemInserted = false;
 static  string value=string.Empty;
 bool isCancelorUpdate = false
 
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
 {
     // To close the edit form on update or cancel
     if ((e.CommandName == RadGrid.CancelCommandName)||(e.CommandName==RadGrid.UpdateCommandName))
     {
         isCancelorUpdate = true;
     }
 }
 protected void Button1_Click(object sender, EventArgs e)
 { //To open Insert form
     RadGrid1.MasterTableView.IsItemInserted = true;
     RadGrid1.MasterTableView.Rebind();      
 }
 
  protected void Button2_Click(object sender, EventArgs e)
 {
     RadGrid grid = (this.FindControl("RadGrid1") as RadGrid);
     if (grid.MasterTableView.IsItemInserted)
     {
         //Firing the InsertCommand
 (grid.MasterTableView.GetItems(GridItemType.CommandItem)[0] as GridCommandItem).FireCommandEvent(RadGrid.PerformInsertCommandName, string.Empty);
     }      
 }
 
 protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
 {
        isItemInserted = true;
      if ( e.Item.OwnerTableView.IsItemInserted)
     {
         GridEditableItem edit = e.Item.OwnerTableView.GetInsertItem();
         TextBox txt = (TextBox)edit["OrderID"].Controls[0];
         value = txt.Text;
         TextBox txt2 = (TextBox)edit["ShipName"].Controls[0];
        //Code to Insert
     }
 }
 
 protected void RadGrid1_PreRender(object sender, EventArgs e)
 {     
     if((value != null)&&(!isCancelorUpdate ))
     {
         foreach (GridDataItem item in RadGrid1.Items)
         {
            string orderid= item["OrderID"].Text;
            if (string.Compare(orderid, value) == 0)
             {
                 item.Edit = true;
                 isItemInserted = false;
                 RadGrid1.Rebind();
             }
         }
     }     
 }

Thanks,
Princy
Tags
Grid
Asked by
MRa
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Share this question
or