here i am showing you the code into which i am getting trouble into "gridWarehouseInsert" command. What i want is when user inser new data into detail table i want to get the MasterTable DataColumn that is "Warehouse_id". so that i can perform insert operation. When user insert master table data that is "warehouse" it works well. but foinserting DetailTable that is "Bin" i am getting trouble in fatching that parent datacolumn primarykey.

Please feel free to ask me any further detail if you need and also feel free to show your openion.

Waiting for your response freiends...

aspx page:

    <telerik:RadGrid ID="GridWarehouseLst" runat="server" ShowStatusBar="true" 
        AutoGenerateColumns="False" AutoGenerateEditColumn="false" AutoGenerateDeleteColumn="false"
        AllowSorting="True" AllowMultiRowSelection="False" AllowPaging="true" PageSize="10" Skin="Windows7" CssClass="gridWarehouseBinInpiutInputStyle"
        OnNeedDataSource="WarehouseBinInpiutList_NeedDataSource" 
        OnItemCommand="gridWarehouseDelete" OnUpdateCommand="gridWarehouseUpdate"  OnInsertCommand="gridWarehouseInsert"
        AllowAutomaticDeletes="True" AllowAutomaticInserts="True" AllowAutomaticUpdates="True" OnDetailTableDataBind="WarehouseBinInpiutList_DetailTableDataBind" >
        <MasterTableView Width="100%" DataKeyNames="Warehouse_id" AllowMultiColumnSorting="True" 
            CommandItemDisplay="Bottom" insertitempageindexaction="ShowItemOnLastPage" EditMode="InPlace" InsertItemDisplay="Bottom">
            <Columns>
                <telerik:GridBoundColumn SortExpression="Warehouse_id" HeaderText="Warehouse Id" HeaderButtonType="TextButton"
                    DataField="Warehouse_id" Visible="false" ReadOnly="True">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn SortExpression="Warehouse_name" HeaderText="Warehouse / Location" HeaderButtonType="TextButton"
                    DataField="Warehouse_name" ReadOnly="False">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn SortExpression="totBins" HeaderText="Total Bins" HeaderButtonType="TextButton"
                    DataField="totBins" ReadOnly="True" ItemStyle-Width="100px">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn SortExpression="totStorage" HeaderText="Storage" HeaderButtonType="TextButton"
                    DataField="totStorage" ReadOnly="True" ItemStyle-Width="100px">
                </telerik:GridBoundColumn>
                <telerik:GridEditCommandColumn HeaderText="Edit" EditImageUrl="images/edit.png" EditText="" UpdateText="" CancelText="" ItemStyle-CssClass="editBoxStyle1" UpdateImageUrl="images/save.png" CancelImageUrl="images/cancel.png" ButtonType="ImageButton"></telerik:GridEditCommandColumn>
                <telerik:GridTemplateColumn HeaderText="Delete" ItemStyle-Width="50px">
                    <ItemTemplate>
                        <asp:ImageButton ID="imgBtnDeleteWarehouse"  runat="server" CommandName="deleteWarehouse" ImageUrl="~/Inventory/images/delete-icon.png" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
            <DetailTables>
                <telerik:GridTableView DataKeyNames="bin_id" Name="Bin" Width="983px" CommandItemDisplay="Bottom" insertitempageindexaction="ShowItemOnLastPage" EditMode="InPlace" InsertItemDisplay="Bottom" EditFormSettings-EditColumn-Display="true">
                    <Columns>
                        <telerik:GridBoundColumn SortExpression="bin_id" HeaderText="Bin ID" HeaderButtonType="TextButton"
                            DataField="bin_id" Visible="false"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn SortExpression="Warehouse_fkid" HeaderText="Bin ID" HeaderButtonType="TextButton"
                            DataField="Warehouse_fkid" Visible="false"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn SortExpression="bin_name" HeaderText="Bin Name" HeaderButtonType="TextButton"
                            DataField="bin_name" UniqueName="bin_name" DataFormatString="{0:D}"></telerik:GridBoundColumn>
                        <telerik:GridEditCommandColumn HeaderText="Edit" EditImageUrl="images/edit.png" EditText="" UpdateText="" CancelText="" ItemStyle-CssClass="editBoxStyle1" UpdateImageUrl="images/save.png" CancelImageUrl="images/cancel.png" ButtonType="ImageButton"></telerik:GridEditCommandColumn>
                        <telerik:GridTemplateColumn HeaderText="Delete" ItemStyle-Width="50px" ReadOnly="true">
                            <ItemTemplate>
                                <asp:ImageButton ID="imgBtnDeleteBin"  runat="server" CommandName="deleteBin" ImageUrl="~/Inventory/images/delete-icon.png" />
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                    </Columns>
                    <CommandItemSettings AddNewRecordText="Add New BIN" ShowRefreshButton="true" />
                </telerik:GridTableView>
            </DetailTables>
            <CommandItemSettings AddNewRecordText="Add New Warehouse" ShowRefreshButton="true" />
        </MasterTableView>
        <clientsettings allowcolumnsreorder="True" reordercolumnsonclient="True" KeyboardNavigationSettings-AllowSubmitOnEnter="True" EnableRowHoverStyle="True">
            <selecting allowrowselect="True" />
            <ClientEvents OnRowDblClick="RowDblClick"></ClientEvents>
        </clientsettings>

    </telerik:RadGrid>
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            function RowDblClick(sender, eventArgs) {
                sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
            }
        </script>
    </telerik:RadCodeBlock>



cs page:

    protected void WarehouseBinInpiutList_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        //if (!e.IsFromDetailTable) GridWarehouseBinInpiutInput.DataSource = from objPrdCat in objmain.setupprodcategories where objPrdCat.companies_fkid.ToString() == Session["Comp_fkid"].ToString() && objPrdCat.divisions_fkid.ToString() == Session["division_fkid"].ToString() select objPrdCat;
        if (!e.IsFromDetailTable)
        {
            var objAllWareHouse  = from objWarehouse in objmain.tbl_Warehouses
                                          where objWarehouse.companies_fkid.ToString() == Session["Comp_fkid"].ToString() && objWarehouse.divisions_fkid.ToString() == Session["division_fkid"].ToString() && objWarehouse.user_fkid.ToString() == Session["User_id"].ToString()
                                          select new
                                          {
                                              objWarehouse.Warehouse_id,
                                              objWarehouse.Warehouse_name,
                                              objWarehouse.user_fkid,
                                              objWarehouse.divisions_fkid,
                                              objWarehouse.companies_fkid,
                                              totStorage = "0",
                                              totBins = (from objBins in objmain.tbl_binDetails where objBins.Warehouse_fkid == objWarehouse.Warehouse_id select objBins).Count()
                                          };
            GridWarehouseBinInpiutInput.DataSource = objAllWareHouse;
            GridWarehouseLst.DataSource = objAllWareHouse;
            //gridWarehouseBinList.DataSource = objAllWareHouse;
        }
    }

    protected void WarehouseBinInpiutList_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
        if (e.DetailTableView.Name == "Bin")
        {
            e.DetailTableView.DataSource = from objBins in objmain.tbl_binDetails
                                           where objBins.Warehouse_fkid == int.Parse(dataItem.GetDataKeyValue("Warehouse_id").ToString())
                                           select new
                                           {
                                               objBins.bin_id,
                                               objBins.bin_name,
                                               objBins.Warehouse_fkid
                                           };
        }
    }

    protected void WarehouseBinInpiutList_PreRender(object sender, EventArgs e)
    {

    }

    protected void gridWarehouseUpdate(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "Update")
        {
            //GridDataItem dataItem = (GridDataItem)e.Item;
            //string s = dataItem.GetDataKeyValue("Warehouse_name").ToString(); 
            GridEditableItem editedItem = e.Item as GridEditableItem;
            GridEditManager editMan = editedItem.EditManager;

            foreach (GridColumn column in e.Item.OwnerTableView.RenderColumns)
            {
                if (column is IGridEditableColumn)
                {
                    IGridEditableColumn editableCol = (column as IGridEditableColumn);
                    if (editableCol.IsEditable)
                    {
                        IGridColumnEditor editor = editMan.GetColumnEditor(editableCol);

                        object editorValue = null;

                        if (editor is GridTextColumnEditor)
                        {
                            editorValue = (editor as GridTextColumnEditor).Text;
                        }

                        if (editor is GridBoolColumnEditor)
                        {
                            editorValue = (editor as GridBoolColumnEditor).Value;
                        }

                        if (editor is GridDropDownColumnEditor)
                        {
                            editorValue = (editor as GridDropDownColumnEditor).SelectedValue;
                        }
                        if (editor is GridDateTimeColumnEditor)
                        {
                            editorValue = (editor as GridDateTimeColumnEditor).Text;
                        }

                        try
                        {
                            //update query
                            objmain.updateWarehouse(int.Parse(editedItem.GetDataKeyValue("Warehouse_id").ToString()), int.Parse(Session["Comp_fkid"].ToString()), int.Parse(Session["division_fkid"].ToString()), editorValue.ToString());
                            rebindData();

                            popupMsg.InnerText = "Warehouse has been updated successfully.";
                            msgPopup.Visible = true;
                            editedItem.Edit = false;
                        }
                        catch (Exception ex)
                        {
                            popupMsg.InnerText = "An Error Occoured during updation of warehouse.";
                            msgPopup.Visible = true;
                            //RadGrid1.Controls.Add(new LiteralControl("<strong>Unable to set value of column '" + column.UniqueName + "'</strong> - " + ex.Message));
                            //e.Canceled = true;
                            //break;
                        }
                    }
                }
            }
        }
    }

    protected void gridWarehouseInsert(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "PerformInsert")
        {
            GridEditableItem editItem = (GridEditableItem)e.Item;
            TextBox OrderText = new TextBox();
            string isType = "";
            if (isType == "")
            {
                try
                {
                    OrderText = (TextBox)editItem["Warehouse_name"].Controls[0];
                    isType = "warehouse";
                }
                catch (Exception ex) { }
            }
            if (isType == "")
            {
                try
                {
                    OrderText = (TextBox)editItem["bin_name"].Controls[0];
                    isType = "bin";
                }
                catch (Exception ex) { }
            }
            if (isType == "")
            {
                try
                {
                    OrderText = (TextBox)editItem["product_id"].Controls[0];
                    isType = "product";
                }
                catch (Exception ex) { }
            }

            GridDataInsertItem newItem = e.Item as GridDataInsertItem;
            GridEditManager insertMannager = newItem.EditManager;

            foreach (GridColumn column in e.Item.OwnerTableView.RenderColumns)
            {
                if (column is IGridEditableColumn)
                {
                    IGridEditableColumn editableCol = (column as IGridEditableColumn);
                    if (editableCol.IsEditable)
                    {
                        IGridColumnEditor editor = insertMannager.GetColumnEditor(editableCol);
                        object editorValue = null;
                        if (editor is GridTextColumnEditor)
                        {
                            editorValue = (editor as GridTextColumnEditor).Text;
                        }
                        if (editorValue != "")
                        {
                            try
                            {
                                //update query
                                objmain.insertWarehouse(editorValue.ToString(), int.Parse(Session["Comp_fkid"].ToString()), int.Parse(Session["division_fkid"].ToString()), int.Parse(Session["User_id"].ToString()));
                                rebindData();
                                popupMsg.InnerText = "New warehouse has been inserted successfully.";
                                msgPopup.Visible = true;
                            }
                            catch (Exception ex)
                            {
                                popupMsg.InnerText = "An Error Occoured during insertion of new warehouse.";
                                msgPopup.Visible = true;
                            }
                        }
                    }
                }
            }
        }
    }

    protected void gridWarehouseDelete(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == "deleteWarehouse")
        {
            GridDataItem dataItem = (GridDataItem)e.Item;
            int totBins = totBins = (from objBins in objmain.tbl_binDetails where objBins.Warehouse_fkid == int.Parse(dataItem.GetDataKeyValue("Warehouse_id").ToString()) select objBins).Count();
            if ((from objBins in objmain.tbl_binDetails where objBins.Warehouse_fkid == int.Parse(dataItem.GetDataKeyValue("Warehouse_id").ToString()) select objBins).Count() == 0)
            {
                //popupConfirmDelete.Visible = true;
                objmain.deleteWarehouse(int.Parse(dataItem.GetDataKeyValue("Warehouse_id").ToString()), int.Parse(Session["Comp_fkid"].ToString()), int.Parse(Session["division_fkid"].ToString()));
                popupMsg.InnerText = "Warehouse has been deleted successfully.";
                msgPopup.Visible = true;
                rebindData();
            }
            else
            {
                popupMsg.InnerText = "The warehouse is not empty. Please delete all Bins before deleting this warehouse.";
                msgPopup.Visible = true;
            }                
        }
    }

    protected void btnConfirmDelWarehouse_Click(object sender, EventArgs e)
    {
        popupConfirmDelete.Visible = false;

    }
    protected void btnCancelDelWarehouse_Click(object sender, EventArgs e)
    {
        popupConfirmDelete.Visible = false;
    }

    protected void btnCloseMsgPopupBx(object sender, EventArgs e)
    {
        msgPopup.Visible = false;
    }

    void rebindData()
    {
        GridWarehouseLst.MasterTableView.Rebind();
        GridWarehouseBinInpiutInput.MasterTableView.Rebind();
        //gridWarehouseBinList.MasterTableView.Rebind();
    }

Recommended Answers

All 3 Replies

Member Avatar for LastMitch

here i am showing you the code into which i am getting trouble into "gridWarehouseInsert" command. What i want is when user inser new data into detail table i want to get the MasterTable DataColumn that is "Warehouse_id". so that i can perform insert operation. When user insert master table data that is "warehouse" it works well. but foinserting DetailTable that is "Bin" i am getting trouble in fatching that parent datacolumn primarykey.

Which example are you using?

Take a look at these demo which is active (to get an idea how to resolve your issue):

http://demos.telerik.com/aspnet-ajax/grid/examples/overview/defaultcs.aspx

No, this example is not have add edit delete commands features. Can you please analyse my code that i have mention above and give me your reviews... i want to find the master table column id("Warehouse_id") on "gridWarehouseInsert" command.

Member Avatar for LastMitch

Can you please analyse my code that i have mention above and give me your reviews... i want to find the master table column id("Warehouse_id") on "gridWarehouseInsert" command.

I did. The issue you are having is that you can't retrieving primary key field values for selected items which is your column id?

Where is the query to fetch that Warehouse_id? I mean you wrote the code where is the query for that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.