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

RadGrid Not saving to database

1 Answer 198 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Matthew
Top achievements
Rank 1
Matthew asked on 20 Jan 2015, 07:11 PM
So I am trying to use a radgrid to allow updates to my database, and while the page functions properly, I can't seem to save the data back to the database, any help you can offer would be appreciated greatly. I am new to both C# and asp.

Page Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ValidationEdit.aspx.cs" Inherits="EditPages_ValidationEdit" MasterPageFile="~/EUEMain.master"   %>
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
 
 
<asp:Content ID="Content1" ContentPlaceHolderID="cphLeftNav" runat="Server">
    <asp:HyperLink ID="HomeLink" runat="server" NavigateUrl="..\Default.aspx" Text="Home" />
    <br />
    <asp:HyperLink ID="AddLink" runat="server" Text="Status Edit Page" />
    <br />
</asp:Content>
 
<asp:Content ID="Content2" ContentPlaceHolderID="cphContent" runat="Server">
    <p></p>
<p></p>
<p></p>
    <h1>Subdivision Statuses</h1>
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<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 ID="RadGrid1" runat="server" DataModelID="Status" AllowPaging="True" AllowAutomaticUpdates="True" AllowAutomaticInserts="True"
        AllowAutomaticDeletes="true" AllowSorting="true" OnItemCreated="RadGrid1_ItemCreated"
        OnItemInserted="RadGrid1_ItemInserted" OnPreRender="RadGrid1_PreRender" OnInsertCommand="RadGrid1_InsertCommand" OnNeedDataSource="Radgrid1_NeedDataSource">
         
    <PagerStyle Mode="NextPrevAndNumeric" />
        <MasterTableView  AutoGenerateColumns="False"
            DataKeyNames="Description" CommandItemDisplay="Top">
            <Columns>
                <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                </telerik:GridEditCommandColumn>
                <telerik:GridBoundColumn DataField="Description" HeaderText="Description" SortExpression="Description"
                    UniqueName="Description" >
                </telerik:GridBoundColumn>
                <telerik:GridCheckBoxColumn  DataField="Visible" HeaderText="Visible" SortExpression="Visible"
                    UniqueName="Visible">
                </telerik:GridCheckBoxColumn>
                <telerik:GridBoundColumn DataField="StatusType" HeaderText="Status Type" UniqueName="StatusType"></telerik:GridBoundColumn>
                <telerik:GridButtonColumn Text="Delete" CommandName="Delete" ButtonType="ImageButton" />
            </Columns>
            <EditFormSettings>
                <EditColumn ButtonType="ImageButton" />
            </EditFormSettings>
        </MasterTableView>
 
    </telerik:RadGrid>
    <h1>
        Track Statuses
        <telerik:RadGrid ID="RadGrid2" runat="server" DataModelID="Status" AllowPaging="True" AllowAutomaticUpdates="True" AllowAutomaticInserts="True"
        AllowAutomaticDeletes="true" AllowSorting="true" OnItemCreated="RadGrid1_ItemCreated"
        OnItemInserted="RadGrid1_ItemInserted" OnPreRender="RadGrid1_PreRender" OnInsertCommand="RadGrid1_InsertCommand" OnNeedDataSource="Radgrid1_NeedDataSource">
         
    <PagerStyle Mode="NextPrevAndNumeric" />
        <MasterTableView  AutoGenerateColumns="False"
            DataKeyNames="Description" CommandItemDisplay="Top">
            <Columns>
                <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                </telerik:GridEditCommandColumn>
                <telerik:GridBoundColumn DataField="Description" HeaderText="Description" SortExpression="Description"
                    UniqueName="Description" >
                </telerik:GridBoundColumn>
                <telerik:GridCheckBoxColumn DataField="Visible" HeaderText="Visible" SortExpression="Visible"
                    UniqueName="Visible">
                </telerik:GridCheckBoxColumn>
                <telerik:GridBoundColumn DataField="StatusType" HeaderText="Status Type" UniqueName="StatusType"></telerik:GridBoundColumn>
                 
                <telerik:GridButtonColumn Text="Delete" CommandName="Delete" ButtonType="ImageButton" />
            </Columns>
            <EditFormSettings>
                <EditColumn ButtonType="ImageButton" />
            </EditFormSettings>
        </MasterTableView>
 
    </telerik:RadGrid>
    </h1>
     <asp:EntityDataSource ID="EntityDataSourceStatus" runat="server" ConnectionString="name=PtcDbModelEntities"
        DefaultContainerName="PtcDbModelEntities" EntitySetName="Status" OrderBy="it.[ContactName]"
        EntityTypeFilter="Description" EnableDelete="True" EnableFlattening="False" EnableInsert="True" EnableUpdate="True">
    </asp:EntityDataSource>
</asp:Content>

and the Code behind:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using Model;
using Telerik.Web.UI;
 
public partial class EditPages_ValidationEdit : BNSF.EUECommonApplication.EUEBasePage
{
 
    public List<Status> StatusList { get; set; }
    public object CurrentSubStatusList { get; set; }
 
    protected void Page_Load(object sender, EventArgs e)
    {
         
        AddLink.NavigateUrl = @"..\EditPages\ValidationEdit";
        if (!Page.IsPostBack)
        {
             
                using (PtcDbModel localDb = new PtcDbModel())
                {
                    var localTemp = (from tempList in localDb.Status
                                     where tempList.StatusType == "S"
                                     select tempList)
                                    .ToList();
                    var localTemp2 = (from tempList in localDb.Status
                                      where tempList.StatusType == "T"
                                      select tempList)
                                    .ToList();
                    CurrentSubStatusList = localTemp;
                    CurrentTrackStatusList = localTemp2;
 
                    var tempDetailList = CurrentSubStatusList;
                    var tempDetailList2 = CurrentTrackStatusList;
                    RadGrid1.DataSource = tempDetailList;
                    RadGrid2.DataSource = tempDetailList2;
                }
            }
        }
 
    public List<Status> CurrentTrackStatusList { get; set; }
 
 
    protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            if (!(e.Item is GridEditFormInsertItem))
            {
                GridEditableItem item = e.Item as GridEditableItem;
                GridEditManager manager = item.EditManager;
                GridTextBoxColumnEditor editor = manager.GetColumnEditor("Description") as GridTextBoxColumnEditor;
                editor.TextBoxControl.Enabled = true;
            }
        }
    }
    protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
    {
        if (e.Exception != null)
        {
 
            e.ExceptionHandled = true;
            SetMessage("Customer cannot be inserted. Reason: " + e.Exception.Message);
 
        }
        else
        {
            SetMessage("New customer is inserted!");
        }
    }
    private void DisplayMessage(string text)
    {
        RadGrid1.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));
    }
 
    private void SetMessage(string message)
    {
        gridMessage = message;
    }
 
    private string gridMessage = null;
 
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(gridMessage))
        {
            DisplayMessage(gridMessage);
            RadGrid1.DataBind();
            RadGrid2.DataBind();
        }
    }
 
    protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
    {
        if (e.Item is GridEditableItem)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            //here editedItem.SavedOldValues will be the dictionary which holds the
            //predefined values
 
            //Prepare new dictionary object
            Hashtable newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            //the newValues instance is the new collection of key -> value pairs
            //with the updated ny the user data
        }
    }
 
 
    protected void Radgrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        using (PtcDbModel localDb = new PtcDbModel())
        {
            var localTemp = (from tempList in localDb.Status
                             where tempList.StatusType == "S"
                             select tempList)
                            .ToList();
            
 
            var localTemp2 = (from tempList in localDb.Status
                              where tempList.StatusType == "T"
                              select tempList)
                                     .ToList();
            CurrentSubStatusList = localTemp;
            CurrentTrackStatusList = localTemp2;
 
            var tempDetailList = CurrentSubStatusList;
            var tempDetailList2 = CurrentTrackStatusList;
            RadGrid1.DataSource = tempDetailList;
            RadGrid2.DataSource = tempDetailList2;
 
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 23 Jan 2015, 08:19 AM
Hello Matthew,

I see that you are setting the AllowAutomaticUpdates, AllowAutomaticInserts and AllowAutomaticDeletes properties of the RadGrid to true, but the automatic CRUD operations will work only when you are binding the grid to a properly configured DataSource control and not through the NeedDataSource event. If you are using the Advanced Data-Binding through the OnNeedDataSource event of the grid, you will have to manually update your data. 

Detailed information on the automatic CRUD operations and the manual updates is available in the following help articles:
Hope this helps.


Best Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Matthew
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or