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

RadGrid with RadTabStrip using EntityDataSource

3 Answers 73 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ryan
Top achievements
Rank 1
Ryan asked on 05 Jul 2012, 09:22 PM
I've tried to make this work correctly using multiple methods, however when a user would click the Edit Button on one of the Internal RadGrid items it loses track of the row that is being edited. Thus shows in the background all possible rows, and does not know what the values were of the record being edited.

Attached is the ASPX and Code Behind for reference.

<%@ Page Title="" Language="C#" AutoEventWireup="true" CodeBehind="Test2.aspx.cs"
    Inherits="mtc_knowledgebase.Test2" %>
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<head id="Head1" runat="server">
    <style type="text/css">
        span.riSingle
        {
            position: static;
        }
    </style>
</head>
<body class="BODY">
    <form runat="server" id="mainForm" method="post">
    <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
    <telerik:RadGrid AllowSorting="True" AutoGenerateColumns="False" DataSourceID="DNDataSource"
        ID="DNGrid" runat="server" GridLines="Both" AllowPaging="true" AllowFilteringByColumn="true"
        CssClass="span.riSingle" OnItemCommand="DNGrid_OnItemCommand">
        <MasterTableView CommandItemDisplay="Bottom" DataKeyNames="dn_hdr_id" EditMode="PopUp"
            Name="HeaderGrid" AllowAutomaticInserts="true" AllowAutomaticUpdates="true">
            <NestedViewTemplate>
                <asp:Panel runat="server" ID="InnerContainer">
                    <telerik:RadTabStrip runat="server" ID="TabStip1" MultiPageID="Multipage1" SelectedIndex="0"
                        DataFieldParentID="dn_hdr_id">
                        <Tabs>
                            <telerik:RadTab runat="server" Text="DN Items" PageViewID="ItemView">
                            </telerik:RadTab>
                            <telerik:RadTab runat="server" Text="DN Approvals" PageViewID="ApprovalView">
                            </telerik:RadTab>
                        </Tabs>
                    </telerik:RadTabStrip>
                    <telerik:RadMultiPage runat="server" ID="Multipage1" SelectedIndex="0" RenderSelectedPageOnly="false">
                        <telerik:RadPageView runat="server" ID="ItemView">
                            <telerik:RadGrid AllowSorting="True" AutoGenerateColumns="False" DataSourceID="DNDtlDataSource"
                                ID="DetailGrid" runat="server" GridLines="Both" CssClass="span.riSingle" BackColor="Azure"
                                HeaderStyle-ForeColor="DimGray" HeaderStyle-BorderColor="DarkGoldenrod" HeaderStyle-BorderWidth="2px"
                                OnInsertCommand="GridOnInsert" OnUpdateCommand="GridOnUpdate" OnItemCommand="GridGetId">
                                <MasterTableView CommandItemDisplay="Bottom" DataKeyNames="dn_dtl_id,dn_hdr.dn_hdr_id"
                                    EditMode="PopUp" Name="DetailGrid">
                                    <CommandItemSettings ShowExportToExcelButton="false" ShowRefreshButton="false" AddNewRecordText="Add New Detail Record" />
                                    <EditFormSettings EditColumn-ButtonType="PushButton" EditColumn-UpdateText="Process Update"
                                        PopUpSettings-Modal="true" PopUpSettings-Width="85%" />
                                    <Columns>
                                        <telerik:GridBoundColumn UniqueName="DTL_DN_DTL_ID" DataField="dn_dtl_id" ReadOnly="true"
                                            Visible="false" />
                                        <telerik:GridEditCommandColumn EditText="EDIT" UniqueName="btnEdit" ButtonType="PushButton" />
                                        <telerik:GridAttachmentColumn DataSourceID="DNDtlDataSource2" MaxFileSize="10485760"
                                            EditFormHeaderTextFormat="Upload File:" HeaderText="Attachment" AttachmentDataField="dn_file_att"
                                            AttachmentKeyFields="dn_dtl_id" FileNameTextField="dn_file_nm" DataTextField="dn_file_nm"
                                            UniqueName="DTL_AttachmentColumn" AllowedFileExtensions=".pdf,.jpg" />
                                        <telerik:GridBoundColumn UniqueName="DTL_CREAT_BY" HeaderText="Created By" DataField="creat_by"
                                            ReadOnly="true" />
                                        <telerik:GridBoundColumn UniqueName="DTL_CREAT_DT" HeaderText="Date Created" DataField="creat_dt"
                                            ReadOnly="true" DataFormatString="{0:MM/dd/yy}" />
                                        <telerik:GridBoundColumn UniqueName="DTL_UPDT_BY" HeaderText="Last Updated By" DataField="updt_by"
                                            ReadOnly="true" />
                                        <telerik:GridBoundColumn UniqueName="DTL_UPDT_DT" HeaderText="Last Updated" DataField="updt_dt"
                                            DataFormatString="{0:MM/dd/yy}" ReadOnly="true" />
                                    </Columns>
                                </MasterTableView>
                            </telerik:RadGrid>
                        </telerik:RadPageView>
                        <telerik:RadPageView runat="server" ID="ApprovalView">
                            <telerik:RadGrid AllowSorting="True" AutoGenerateColumns="False" ID="ApprovalGrid"
                                DataSourceID="DNDataSource2" runat="server" GridLines="Both" CssClass="span.riSingle"
                                BackColor="Azure" EnableViewState="true" HeaderStyle-ForeColor="DimGray" HeaderStyle-BorderColor="DarkGoldenrod"
                                HeaderStyle-BorderWidth="2px">
                                <MasterTableView CommandItemDisplay="Bottom" DataKeyNames="dn_hdr_id" EditMode="PopUp"
                                    Name="ApprovalGridMaster">
                                    <CommandItemSettings ShowExportToExcelButton="false" ShowRefreshButton="false" ShowAddNewRecordButton="false" />
                                    <EditFormSettings EditColumn-ButtonType="PushButton" EditColumn-UpdateText="Process Update"
                                        PopUpSettings-Modal="true" PopUpSettings-Width="85%" />
                                    <Columns>
                                        <telerik:GridEditCommandColumn EditText="EDIT" UniqueName="btnEdit" ButtonType="PushButton" />
                                        <telerik:GridBoundColumn UniqueName="QR_NM" HeaderText="Quality Signoff" DataField="qr_nm" />
                                        <telerik:GridDateTimeColumn UniqueName="QR_DT" HeaderText="Quality Signoff Date"
                                            DataField="qr_dt" DataFormatString="{0:MM/dd/yy}" />
                                    </Columns>
                                </MasterTableView>
                            </telerik:RadGrid>
                        </telerik:RadPageView>
                    </telerik:RadMultiPage>
                </asp:Panel>
            </NestedViewTemplate>
            <Columns>
                <telerik:GridEditCommandColumn EditText="EDIT" UniqueName="btnEdit" ButtonType="PushButton" />
                <telerik:GridBoundColumn UniqueName="dn_hdr_id" HeaderText="DN ID" DataField="dn_hdr_id"
                    ReadOnly="true" />
                <telerik:GridBoundColumn UniqueName="SUP_NM" HeaderText="Operator/Supervisor" DataField="sup_nm" />
                <telerik:GridDateTimeColumn UniqueName="SUP_DT" HeaderText="O/S Date" DataField="sup_dt"
                    DataFormatString="{0:MM/dd/yy}" />
            </Columns>
        </MasterTableView>
        <ClientSettings AllowKeyboardNavigation="true">
            <Scrolling AllowScroll="true" ScrollHeight="600px" />
        </ClientSettings>
        <ExportSettings ExportOnlyData="true" IgnorePaging="true" />
    </telerik:RadGrid>
    <asp:EntityDataSource ID="DNDataSource" runat="server" ConnectionString="name=mtc_kbaseEntities"
        DefaultContainerName="mtc_kbaseEntities" EntitySetName="dn_hdr" OrderBy="it.updt_dt"
        Include="cust, prt, orig_1" EnableInsert="true" EnableUpdate="true">
    </asp:EntityDataSource>
    <asp:EntityDataSource ID="DNDataSource2" runat="server" ConnectionString="name=mtc_kbaseEntities"
        DefaultContainerName="mtc_kbaseEntities" EntitySetName="dn_hdr" EnableUpdate="true">
    </asp:EntityDataSource>
    <asp:EntityDataSource ID="DNDtlDataSource2" runat="server" ConnectionString="name=mtc_kbaseEntities"
        DefaultContainerName="mtc_kbaseEntities" EntitySetName="dn_dtl">
    </asp:EntityDataSource>
    <asp:EntityDataSource ID="DNDtlDataSource" runat="server" ConnectionString="name=mtc_kbaseEntities"
        DefaultContainerName="mtc_kbaseEntities" EntitySetName="dn_dtl" Include="orig_2">
    </asp:EntityDataSource>
    </form>
</body>
</html>

Code Behind
using System;
using System.Data;
using System.Linq;
using System.Web;
using Telerik.Web.UI;
  
namespace mtc_knowledgebase
{
    public partial class Test2 : System.Web.UI.Page
    {
        #region Variables
        private int _dnHdrId;
        private int _dnDtlId;
        private string _fileName;
        private byte[] _fileData;
        private string _curUser;
        private DateTime? _curDt;
        private int _errLevel;
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            DNDtlDataSource2.Where = "it.dn_dtl_id == 0";
        }
  
        /// <summary>
        /// This will be used in place of the auto insert event based on File Attachment requirements
        /// </summary>
        /// <param name="sender">
        /// This method is called on the Grid Insert Event
        /// </param>
        /// <param name="e">
        /// The entire Inserted record will be passed
        /// </param>
        protected void GridOnInsert(object sender, GridCommandEventArgs e)
        {
            if ("DetailGrid".Equals(e.Item.OwnerTableView.Name))
            {
                var editItem = e.Item as GridEditableItem;
                GetRecordInfo(editItem);
                if (_errLevel == 0)
                {
                    try
                    {
                        using (var db = new mtc_kbaseEntities())
                        {
                            var newDnDtl = new dn_dtl
                            {
                                dn_hdrReference = { EntityKey = new EntityKey("mtc_kbaseEntities.dn_hdr", "dn_hdr_id", _dnHdrId) },
                                dn_file_nm = _fileName,
                                dn_file_att = _fileData,
                                creat_by = _curUser,
                                creat_dt = _curDt,
                                updt_by = _curUser,
                                updt_dt = _curDt
                            };
                            db.AddTodn_dtl(newDnDtl);
                            db.SaveChanges();
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Write("<script type=\"text/javascript\" language=\"javascript\">");
                        Response.Write("alert('" + ex.Message + "');");
                        Response.Write("</script>");
                    }
                }
            }
        }
  
        /// <summary>
        /// This will be used in place of the auto update event based on File Attachment requirements
        /// </summary>
        /// <param name="sender">
        /// This method is called on the Grid Update Event
        /// </param>
        /// <param name="e">
        /// The entire Updated record will be passed
        /// </param>
        protected void GridOnUpdate(object sender, GridCommandEventArgs e)
        {
            if ("DetailGrid".Equals(e.Item.OwnerTableView.Name))
            {
                var editItem = e.Item as GridEditableItem;
                _dnDtlId = Convert.ToInt32((editItem.EditManager.GetColumnEditor("DTL_DN_DTL_ID") as GridTextBoxColumnEditor).Text);
                GetRecordInfo(editItem);
                if (_errLevel == 0)
                {
                    try
                    {
                        using (var db = new mtc_kbaseEntities())
                        {
                            var updDtl = db.dn_dtl.First(t => t.dn_dtl_id == _dnDtlId);
                            updDtl.dn_file_nm = _fileName;
                            updDtl.dn_file_att = _fileData;
                            updDtl.updt_by = _curUser;
                            updDtl.updt_dt = _curDt;
                            db.SaveChanges();
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Write("<script type=\"text/javascript\" language=\"javascript\">");
                        Response.Write("alert('" + ex.Message + "');");
                        Response.Write("</script>");
                    }
                }
            }
        }
  
        /// <summary>
        /// If a request for the file download is initiated, this will update the datasource
        /// </summary>
        /// <param name="sender">
        /// The datasource for the attachment requires the ID of the record
        /// </param>
        /// <param name="e">
        /// Only fire the event if it's a download request
        /// </param>
        protected void GridGetId(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "DownloadAttachment")
            {
                var args = e as GridDownloadAttachmentCommandEventArgs;
                _dnDtlId = (int)args.AttachmentKeyValues["dn_dtl_id"];
                DNDtlDataSource2.Where = "it.dn_dtl_id == " + _dnDtlId;
            }
        }
  
        /// <summary>
        /// This is a common method for setting variables based on the attachment details
        /// </summary>
        /// <param name="editItem">
        /// The edit item.
        /// </param>
        protected void GetRecordInfo(GridEditableItem editItem)
        {
            _errLevel = 0;
            RadUpload upload = ((GridAttachmentColumnEditor)editItem.EditManager.GetColumnEditor("DTL_AttachmentColumn")).RadUploadControl;
            if (upload.InvalidFiles.Count > 0)
            {
                _errLevel = 1;
                Response.Write("<script type=\"text/javascript\" language=\"javascript\">");
                Response.Write("alert('Only PDF or JPG files smaller than 10MB are allowed');");
                Response.Write("</script>");
                return;
            }
  
            if (upload.UploadedFiles.Count != 0)
            {
                _fileName = upload.UploadedFiles[0].GetName();
                _fileData = ((GridAttachmentColumnEditor)editItem.EditManager.GetColumnEditor("DTL_AttachmentColumn")).UploadedFileContent;
            }
  
            _curUser = HttpContext.Current.User.Identity.Name.ToString().Replace("MORGANGROUPPLC\\", string.Empty);
            _curDt = DateTime.Now;
        }
  
        protected void DNGrid_OnItemCommand(object sender, GridCommandEventArgs e)
        {
            GridDataItem grdDataitem = (GridDataItem)e.Item;
  
            if (e.CommandName == RadGrid.ExpandCollapseCommandName)
            {
                _dnHdrId = (int)grdDataitem.GetDataKeyValue("dn_hdr_id");
                DNDtlDataSource.Where = "it.dn_hdr.dn_hdr_id == " + _dnHdrId;
                DNDataSource2.Where = "it.dn_hdr_id == " + _dnHdrId;
            }
        }
    }
}

3 Answers, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 10 Jul 2012, 11:09 AM
Hello,

I am not sure that I understand correctly what is the problem. Could you elaborate a bit more on which grid you click and the steps needed to reproduce the erroneous behavior? Sample video could help to better understand your case.


Regards,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Ryan
Top achievements
Rank 1
answered on 10 Jul 2012, 04:34 PM
By altering the DataSource logic to utilize the NeedDataSource command, and rewriting a few other portions I was able to solve my issue.
0
Andrey
Telerik team
answered on 13 Jul 2012, 12:09 PM
Hello,

Yes, the NeedDataSource event is the preferable way for binding RadGrid.

I am glad that the issue you were facing is now resolved. Do not hesitate to contact us again if you need further assistance.

Greetings,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Ryan
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Ryan
Top achievements
Rank 1
Share this question
or