Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
860
Child Row will not Delete!
posted

I am attempting to delete a child row on a hierarchical Data Grid but i keep get an error: Requested record cannot be found by key.  I have no problems with insert,update,or delete on parent and no problems with update or insert on child.. just delete.  I am programatically setting the data source.   I have supplied the source code for this page.  I have run out of ideas... need help

BLOCKED SCRIPT

function DeleteChildRows() {
    var grid = $find('whdgRatings');
    var gridRows = grid.get_gridView().get_rows();
    var selectedRows;
    var row;
    var childGrid;

    for (var j = 0; j < gridRows.get_length(); j++) {
        row = gridRows.get_row(j);

        childGrid = row.get_rowIslands()[0];
              
        selectedRows = childGrid.get_behaviors().get_selection().get_selectedRows();

        if (selectedRows.get_length() > 0) {


            for (var i = selectedRows.get_length() - 1; i >= 0; i--) {

                row = selectedRows.getItem(i);
                gridRows.remove(row);
            }
            // Single Select allowed
            break;
        }

    }

}
function DeleteRow() {
    var grid = $find('whdgRatings');

    var gridRows = grid.get_gridView().get_rows();

    var selectedRows = grid.get_gridView().get_behaviors().get_selection().get_selectedRows();


    for (var i = selectedRows.get_length() - 1; i >= 0; i--) {
        var row = selectedRows.getItem(i);
        gridRows.remove(row);
    }
}

Grid:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<ig:WebHierarchicalDataGrid ID="whdgRatings" runat="server"
        AutoGenerateColumns="False" DataKeyFields="RATING_SET_ID"
        DataMember="RATINGS" Key="RATINGS"
        AutoGenerateBands="False" InitialDataBindDepth="2"
        onrowadding="whdgRatings_RowAdding" onrowsdeleting="whdgRatings_RowsDeleting"
        onrowupdating="whdgRatings_RowUpdating"
        ItemCssClass="WebDataGridItemCssClass">
        <Bands>
            <ig:Band AutoGenerateColumns="False" DataKeyFields="RATING_SET_ID,RANK"
                DataMember="RATING_SET" Key="RATING_SET_ID,RANK"  onrowadding="whdgRatingSet_RowAdding" onrowsdeleting="whdgRatingSet_RowsDeleting"
        onrowupdating="whdgRatingSet_RowUpdating"  OnRowDeleted="whdgRatingSet_OnRowDeleted"
                ItemCssClass="WebDataGridItemCssClass" Width="575px" >
                <Columns>
                    <ig:TemplateDataField Key="Delete" Width="30px" >
                        <ItemTemplate>
                            <asp:ImageButton runat="server" ID="DeleteItem"  AlternateText="Delete" ImageUrl="/Resources/images/icons/redX.gif" OnClientClick="DeleteChildRows(); return false;" />
                        </ItemTemplate>
                    </ig:TemplateDataField>
                    <ig:BoundDataField DataFieldName="RATING_SET_ID" DataType="System.Int32"
                        Hidden="True" Key="RATING_SET_ID">
                        <Header Text="BoundColumn_0" />
                    </ig:BoundDataField>
                    <ig:BoundDataField DataFieldName="RANK" DataType="System.Int32" Key="RANK"
                        Width="75px">
                        <Header Text="Rank" />
                    </ig:BoundDataField>
                    <ig:BoundDataField DataFieldName="NAME" DataType="System.String" Key="NAME"
                        Width="300px">
                        <Header Text="Name" />
                    </ig:BoundDataField>
                    <ig:BoundDataField DataFieldName="RATING_VALUE" DataType="System.Int32"
                        Key="RATING_VALUE" Width="100px">
                        <Header Text="Rating Value" />
                    </ig:BoundDataField>
                </Columns>
                <Behaviors>
                    <ig:EditingCore EnableInheritance="True">
                        <Behaviors>
                            <ig:CellEditing>
                                <ColumnSettings>
                                    <ig:EditingColumnSetting ColumnKey="RANK" EditorID="nepSpinControl"
                                        ReadOnly="True" />
                                    <ig:EditingColumnSetting ColumnKey="NAME" />
                                    <ig:EditingColumnSetting ColumnKey="RATING_VALUE" EditorID="nepTypedNumeric" />
                                </ColumnSettings>
                                <EditModeActions EnableOnActive="True" MouseClick="Single" />
                            </ig:CellEditing>
                            <ig:RowAdding Alignment="Top">
                                <ColumnSettings>
                                    <ig:RowAddingColumnSetting ColumnKey="RANK" EditorID="nepSpinControl" />
                                    <ig:RowAddingColumnSetting ColumnKey="NAME" />
                                    <ig:RowAddingColumnSetting ColumnKey="RATING_VALUE"
                                        EditorID="nepTypedNumeric" />
                                </ColumnSettings>
                                <AddNewRowClientEvents ExitingEditMode="whdgRatings_RowAdding_ExitingEditMode"
                                    EnteredEditMode="whdgRatings_RowAdding_EnteredEditMode" />
                                <EditModeActions EnableOnActive="True" MouseClick="Single" />
                            </ig:RowAdding>
                            <ig:RowDeleting Enabled="true" />
                           
                        </Behaviors>
                    </ig:EditingCore>
                    <ig:Sorting>
                    </ig:Sorting>
                </Behaviors>
            </ig:Band>
        </Bands>
        <ClientEvents AJAXResponse="whdgRatings_ContainerGrid_AJAXResponse" />
        <EditorProviders>
            <ig:DropDownProvider ID="ddpYesNoSelection">
                <EditorControl DropDownContainerMaxHeight="200px" EnableAnimations="False"
                    EnableDropDownAsChild="False" >
                    <Items>
                        <ig:DropDownItem Selected="False" Text="Yes" Value="Y">
                        </ig:DropDownItem>
                        <ig:DropDownItem Selected="False" Text="No" Value="N">
                        </ig:DropDownItem>
                    </Items>
                </EditorControl>
            </ig:DropDownProvider>
            <ig:NumericEditorProvider ID="nepSpinControl">
                <EditorControl MaxDecimalPlaces="0" MaxValue="1000" MinValue="0"
                    >
                    <Buttons SpinButtonsDisplay="OnRight" SpinDelay="100">
                    </Buttons>
                </EditorControl>
            </ig:NumericEditorProvider>
            <ig:NumericEditorProvider ID="nepTypedNumeric">
                <EditorControl MaxDecimalPlaces="0" MinValue="0" >
                </EditorControl>
            </ig:NumericEditorProvider>
        </EditorProviders>
        <Columns>
            <ig:TemplateDataField Key="Delete" Width="30px" >
                <ItemTemplate>
                    <asp:ImageButton runat="server" ID="DeleteItem"  AlternateText="Delete" ImageUrl="/Resources/images/icons/redX.gif" OnClientClick="DeleteRow(); return false;" />
                </ItemTemplate>
            </ig:TemplateDataField>
            <ig:BoundDataField DataFieldName="RATING_SET_ID" DataType="System.Int32"
                Hidden="True" Key="RATING_SET_ID"  >
                <Header Text="BoundColumn_0" />
            </ig:BoundDataField>
            <ig:BoundDataField DataFieldName="DESCRIPTION" DataType="System.String"
                Key="DESCRIPTION" Width="400px">
                <Header Text="Description" />
            </ig:BoundDataField>
            <ig:BoundDataField DataFieldName="USES_SCORING" DataType="System.String"
                Key="USES_SCORING" Width="150px">
                <Header Text="Uses Scoring?" />
            </ig:BoundDataField>
        </Columns>
        <ErrorTemplate>
            <div>Here</div>
        </ErrorTemplate>
        <Behaviors>
            <ig:Activation Enabled="true">
            </ig:Activation>
            <ig:Sorting>
            </ig:Sorting>
            <ig:Selection CellClickAction="Row" RowSelectType="Single">
            </ig:Selection>
            <ig:EditingCore>
                <Behaviors>
                    <ig:RowDeleting Enabled="true" />
                    <ig:CellEditing>
                        <ColumnSettings>
                            <ig:EditingColumnSetting ColumnKey="DESCRIPTION" />
                            <ig:EditingColumnSetting ColumnKey="USES_SCORING"
                                EditorID="ddpYesNoSelection" />
                        </ColumnSettings>
                        <EditModeActions EnableOnActive="True" MouseClick="Single" />
                    </ig:CellEditing>
                    <ig:RowAdding Alignment="Top">
                        <ColumnSettings>
                            <ig:RowAddingColumnSetting ColumnKey="DESCRIPTION" />
                            <ig:RowAddingColumnSetting ColumnKey="USES_SCORING"
                                EditorID="ddpYesNoSelection" />
                        </ColumnSettings>
                        <EditModeActions EnableOnActive="True" MouseClick="Single" />
                    </ig:RowAdding>
                </Behaviors>
            </ig:EditingCore>
        </Behaviors>
    </ig:WebHierarchicalDataGrid>

 

Code Behind:

         protected void Page_OnLoad(object sender, EventArgs e)
        {


            whdgRatings.DataSource = _db.GetRatings();
           
            if (!this.IsPostBack)
            {
//                Sorting behavior = this.whdgRatings.Behaviors.Sorting;
  //              behavior.SortedColumns.Add(this.whdgRatings.Columns["DESCRIPTION"],
    //                                       Infragistics.Web.UI.SortDirection.Ascending);
            }
           
        }

        #region Parent Events
        protected void whdgRatings_RowAdding(object sender, RowAddingEventArgs e)
        {
            int ratingSetId;
            try
            {
                ratingSetId = _db.InsertRating(e.Values["DESCRIPTION"].ToString(), e.Values["USES_SCORING"].ToString());

                e.Values["RATING_SET_ID"] = ratingSetId;
            }
            catch (System.Exception ie)
            {
                ContainerGrid grid = (ContainerGrid)sender;

                grid.GridResponse.Message = ie.Message;
            }
        }

        protected void whdgRatings_RowsDeleting(object sender, RowDeletingEventArgs e)
        {
            try
            {
                object[] dataKey = e.Row.DataKey;

                _db.DeleteRating(Convert.ToInt32 ( dataKey[0] ) );
            }
            catch (System.Exception ie)
            {
                ContainerGrid grid = (ContainerGrid)sender;

                grid.GridResponse.Message = ie.Message + "<br/>" + ie.InnerException.Message;
            }
        }

        protected void whdgRatings_RowUpdating(object sender, RowUpdatingEventArgs e)
        {
            try
            {
                object[] dataKey = e.Row.DataKey;

                _db.UpdateRating(Convert.ToInt32 (  e.Values["RATING_SET_ID"] ), e.Values["DESCRIPTION"].ToString(), e.Values["USES_SCORING"].ToString());

            }
            catch (System.Exception ie)
            {
                ContainerGrid grid = (ContainerGrid)sender;

                grid.GridResponse.Message = ie.Message;
            }
        }
        #endregion


        #region Child Events
        protected void whdgRatingSet_RowAdding(object sender, RowAddingEventArgs e)
        {
            ContainerGrid grid = sender as ContainerGrid;
            object [] keyId  = grid.ParentRow.DataKey;
            int ratingSetId;
           
            try
            {
                ratingSetId = Convert.ToInt32(keyId[0]);

                _db.InsertRatingSet(ratingSetId, Convert.ToInt32(e.Values["RANK"]), e.Values["NAME"].ToString(), Convert.ToDouble(e.Values["RATING_VALUE"]));
                e.Values["RATING_SET_ID"] = ratingSetId;

            }
            catch (System.Exception ie)
            {
                grid.GridResponse.Message = ie.Message;
            }
        }

        protected void whdgRatingSet_OnRowDeleted(object sender, RowDeletedEventArgs e)
        {
           
        }
        protected void whdgRatingSet_RowsDeleting(object sender, RowDeletingEventArgs e)
        {
            System.Data.DataRowView row;
            ContainerGrid grid;

            grid = sender as ContainerGrid;

            row = e.Row.DataItem as System.Data.DataRowView;

            try
            {
                _db.DeleteRatingSet(Convert.ToInt32(row["RATING_SET_ID"]), Convert.ToInt32(row["RANK"]));

                grid.Rows.RemoveAt(e.Row.Index);
            }
            catch (System.Exception ie)
            {

                grid.GridResponse.Message = ie.Message;
            }
        }

        protected void whdgRatingSet_RowUpdating(object sender, RowUpdatingEventArgs e)
        {

            try
            {
                _db.UpdateRatingSet(Convert.ToInt32(e.Values["RATING_SET_ID"]), Convert.ToInt32(e.Values["RANK"]), e.Values["NAME"].ToString(), Convert.ToDouble(e.Values["RATING_VALUE"]));
            }
            catch (System.Exception ie)
            {
                ContainerGrid grid = (ContainerGrid) sender;

                grid.GridResponse.Message = ie.Message;
               
            }
        }
        #endregion

 

DataSource:

 

 

 

  • 695
    Suggested Answer
    posted

    Hello,

    The problem is in your DeleteChildRows function. When you remove a row from a child grid, you must get a reference to its row collection. Try this:

     for (var j = 0; j < gridRows.get_length(); j++) {
                row = gridRows.get_row(j);

                childGrid = row.get_rowIslands()[0];
                childGridRows = childGrid.get_rows();
                selectedRows = childGrid.get_behaviors().get_selection().get_selectedRows();

                if (selectedRows.get_length() > 0) {


                    for (var i = selectedRows.get_length() - 1; i >= 0; i--) {

                        row = selectedRows.getItem(i);
                        childGridRows.remove(row);
                    }
                    // Single Select allowed
                    break;
                }

    Regards,
    Nikolai DImitrov