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

Hierarchical Grid Prevent Double Inserting/Editing

9 Answers 177 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Josh
Top achievements
Rank 1
Josh asked on 16 Jun 2010, 07:15 PM
Hi support,

I searched the internet and this forum to determine how to prevent the user from double inserting/editing at different hierarchical levels.  That is, I have code now which will prevent the user from double inserting or editing at a particular level of the grid.  However, I would like to prevent it at all levels.  That is, if the user has an insert or edit form at one level and then clicks on an insert or edit form at another level then I want to close the insert or edit form at the first level and show only the insert or edit form at the level the user clicked on last.  The purpose being that only one data entry form is ever shown at any one time.  Is this possible?  How do I do it?

I'm actually surprised that this isn't a commonly asked for setting.  It makes sense to me that in a hierarchical grid,  in order to stem some of the chaos, that the developer would only want one data entry form open at a time.

Thanks,
Josh

9 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 17 Jun 2010, 07:39 AM
Hello Josh,

Check out the sample code below which shows how to close the insert form in MasterTable when the user clicks AddNewRecord button in DetailTable.

ASPX:
<MasterTableView Name="Master" runat="server" AutoGenerateColumns="true" DataKeyNames="EmployeeID" 
        CommandItemDisplay="Top"
        <DetailTables> 
            <telerik:GridTableView Name="GridTableView1" runat="server" AutoGenerateColumns="true" 
                DataKeyNames="EmployeeID" CommandItemDisplay="Top" DataSourceID="SqlDataSource2"
                <ParentTableRelation> 
                    <telerik:GridRelationFields DetailKeyField="EmployeeID" MasterKeyField="EmployeeID" /> 
                </ParentTableRelation> 
                <Columns> 
                    <telerik:GridEditCommandColumn> 
                    </telerik:GridEditCommandColumn> 
                </Columns> 
            </telerik:GridTableView> 
        </DetailTables> 
        <Columns> 
            <telerik:GridEditCommandColumn UniqueName="EditColumn"
            </telerik:GridEditCommandColumn> 
        </Columns> 
 </MasterTableView> 

C#:
    int index; 
    int i = 0; 
 
    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) 
    { 
        if (e.CommandName == "InitInsert" && e.Item.OwnerTableView.Name == "GridTableView1"
        { 
            i = 1;  
            index=e.Item.OwnerTableView.ParentItem.ItemIndex; 
        } 
    } 
   protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        if (RadGrid1.MasterTableView.IsItemInserted) 
        { 
            if (i == 1) 
            { 
                RadGrid1.MasterTableView.IsItemInserted = false;                
                RadGrid1.MasterTableView.Rebind(); 
                RadGrid1.MasterTableView.Items[index].Expanded = true
                (RadGrid1.MasterTableView.Items[index] as GridDataItem).ChildItem.NestedTableViews[0].IsItemInserted = true
                (RadGrid1.MasterTableView.Items[index] as GridDataItem).ChildItem.NestedTableViews[0].Rebind(); 
            } 
        } 
    } 

Thanks,
Princy.
0
Josh
Top achievements
Rank 1
answered on 17 Jun 2010, 07:54 PM
Princy,

Thanks for you quick reply.  This has gotten me started.  My modifications to your code now hides all insert forms except for the one the user has clicked upon as I desired.  However, I haven't been able to get this to work for the edit form. 

Any ideas?

Thanks,
Josh
0
Princy
Top achievements
Rank 2
answered on 18 Jun 2010, 07:15 AM
 Hello Josh,

I hope you want to close insertform as well as editform (for mastertable) when opening insertform for detailstable. Make the following changes in your code and see whether this works for edit form also.

C#:
    int index; 
    int i = 0; 
 
    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) 
    { 
        if ((e.CommandName == "InitInsert" ||e.CommandName=="Edit") && e.Item.OwnerTableView.Name == "GridTableView1"
        { 
            i = 1;  
            index=e.Item.OwnerTableView.ParentItem.ItemIndex; 
        } 
     } 
   protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        if (i == 1) 
        { 
          if (RadGrid1.MasterTableView.IsItemInserted || RadGrid1.EditIndexes.Count > 0) 
           { 
                RadGrid1.MasterTableView.IsItemInserted = false
                RadGrid1.EditIndexes.Clear(); 
                RadGrid1.MasterTableView.Rebind(); 
                RadGrid1.MasterTableView.Items[index].Expanded = true
                (RadGrid1.MasterTableView.Items[index] as GridDataItem).ChildItem.NestedTableViews[0].IsItemInserted = true
                (RadGrid1.MasterTableView.Items[index] as GridDataItem).ChildItem.NestedTableViews[0].Rebind(); 
            } 
         } 
    } 

Thanks,
Princy.
0
zel
Top achievements
Rank 1
answered on 11 Oct 2010, 03:52 AM
Hi Princy,

I have the same problem with Josh, but for my scenario i want to close the editform for mastertable when I click on Edit for detailstable.

How to set the master row as edit mode = false?

Thanks!
Zel

0
Princy
Top achievements
Rank 2
answered on 11 Oct 2010, 10:25 AM
Hello Zel,

Try the following code snippet to close the  editform (for mastertable) when opening editform for detailstable

C#:
  int index;
  int childindex = 0;
  int i = 0;
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == "Edit" && e.Item.OwnerTableView.Name == "GridTableView1")
        {
            i = 1;
            index = e.Item.OwnerTableView.ParentItem.ItemIndex;
            childindex = e.Item.ItemIndex;
        }
    }
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        if (i == 1)
        {
            if (RadGrid1.EditIndexes.Count > 0)
            {
                RadGrid1.EditIndexes.Clear();
                RadGrid1.MasterTableView.Rebind();
                RadGrid1.MasterTableView.Items[index].Expanded = true;
                (RadGrid1.MasterTableView.Items[index] as GridDataItem).ChildItem.NestedTableViews[0].Items[childindex].Edit = true;
                (RadGrid1.MasterTableView.Items[index] as GridDataItem).ChildItem.NestedTableViews[0].Rebind();
            }
        }
    }

Thanks,
Princy.
0
zel
Top achievements
Rank 1
answered on 11 Oct 2010, 10:39 AM
Hi Princy,

I already tried your code and unfortunately there is an error occur in this line

(gridCPDiag.MasterTableView.Items[index] as GridDataItem).ChildItem.NestedTableViews[0].Items[childindex].Edit = true;

Error: Index was out of range. Must be non-negative and less than the size of the collection. 

The value of childindex  = -1.

Thanks!
Zel
0
zel
Top achievements
Rank 1
answered on 12 Oct 2010, 06:54 AM
Hi Princy,

I have found another way to get the row index of selected item. But my problem is, it will not be applied in multiple row edit.

Please check my code below.
Instead of putting childindex = e.Item.ItemIndex; in Item Command event, I put it in Item Databound event.

if (e.Item is GridDataItem && e.Item.OwnerTableView.Name == "Detail2" && hdnCommandName.Value == "det2_editselected")
{
 GridDataItem dataItem = (GridDataItem)e.Item;
  if (dataItem.IsInEditMode)
  {
   childindex = dataItem.ItemIndex;
  }
}

Now, in PreRender event:
if (x == 1)
{
 if (gridCPDiag.MasterTableView.IsItemInserted || gridCPDiag.EditIndexes.Count > 0)
 {
  gridCPDiag.EditIndexes.Clear();
  gridCPDiag.MasterTableView.Rebind();
  gridCPDiag.MasterTableView.Items[index].Expanded = true;
  (gridCPDiag.MasterTableView.Items[index] as GridDataItem).ChildItem.NestedTableViews[0].Items[childindex].Edit = true;
  (gridCPDiag.MasterTableView.Items[index] as GridDataItem).ChildItem.NestedTableViews[0].Rebind();
  }
}

Please help.

Thanks!
Lyzel
0
Princy
Top achievements
Rank 2
answered on 13 Oct 2010, 05:31 AM
Hello Zel,

One suggestion to achieve this would be storing the DataKeyValue of  DetailTable item(which is in edit mode) in an array. Then in PreRender event loops through each DetailTable items and restore its edit state. A sample code is shown below. Feel free to modify the logic according to your requirement.


C#:
int index;
Int64[] childindex = new Int64[10]; //declare integer array to store the DataKeyName
int i = 0;
int limit = 0;
 
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
      if (e.Item is GridEditableItem && e.Item.IsInEditMode && e.Item.OwnerTableView.Name == "Detail2" )
       {
           GridEditableItem editItem = (GridEditableItem)e.Item;
           childindex[limit] = Convert.ToInt64(editItem.GetDataKeyValue("EmployeeID")); // store the DataKeyValue of DetailTable item in an array.
           limit++;
        }
   }
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
   {
       if (e.CommandName == "EditSelected" && e.Item.OwnerTableView.Name == "Detail2")
       {
           i = 1;
           index = e.Item.OwnerTableView.ParentItem.ItemIndex;
       }
   }
protected void RadGrid1_PreRender(object sender, EventArgs e)
   {
    if (i == 1)
       {
           if (RadGrid1.EditIndexes.Count > 0)
           {
               RadGrid1.EditIndexes.Clear();
               RadGrid1.MasterTableView.Rebind();
               RadGrid1.MasterTableView.Items[index].Expanded = true;
               GridTableView detailTable = (GridTableView)(RadGrid1.MasterTableView.Items[index] as GridDataItem).ChildItem.NestedTableViews[0];
               for (int j = 0; j < limit; j++)
                   {
                       foreach (GridDataItem item in detailTable.Items)//loops through each DetailTable row and restore its edit mode
                       {
                           if (Convert.ToInt64(item.GetDataKeyValue("EmployeeID")) == childindex[j])
                               item.Edit = true;
                       }
                   }
               detailTable.Rebind();
           }
       }
  }

Thanks,
Princy.
0
zel
Top achievements
Rank 1
answered on 13 Oct 2010, 09:28 AM
THANK YOU VERY MUCH PRINCY!

The sample code you provided is working.

Cheers!
Zel
Tags
Grid
Asked by
Josh
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Josh
Top achievements
Rank 1
zel
Top achievements
Rank 1
Share this question
or