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

Automatic CRUD RadGrid Tweaked as Desired - Except won't Save

2 Answers 8 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 14 Aug 2014, 08:34 PM
First of all, some things are working correctly.  See the attached screenshot.  You'll note that there is no option to insert and no option to delete.  Also if you go to edit 2 of the three columns are blanked out.   And importantly, the grid is working with our master page which took substantial modification of the automatically created grid's aspx markup (not referring to the code behind) and by automatic grid I mean the one that is created by right clicking in visual studio and choosing a RadGrid with CRUD.

I have also included the aspx file and code behind.  If you look in the code behind it includes the class from the code from the CRUD example on the telerik website (http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/automatic-crud-operations/defaultcs.aspx).  The reason a line is commented out is because uncommenting it  produced a 'does not exist in current context error' regarding radgrid 1.

The other (partial) codebehind class is inherited from another developer who made a similar form in a different part of the application in case it helps.

The question I have is what needs to be done to make the grid save?   Do I have to change an option?  Do I have to modify the C# code?   The more specific the answer, the better.  The answers in my last thread definitely worked.

Here's what happens now.  You hit the save button and the values automatically go back to what they were, without changing.


----- aspx file
<%@ Page Language="C#" MasterPageFile="~/MasterPages/Main.Master"  AutoEventWireup="true"  CodeBehind="GlobalSettings.aspx.cs" Inherits="RadGridAutomaticCrudOperations" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cphCenter" runat="server">
    <telerik:RadCodeBlock runat="server" ID="RadCodeBlock1">
        <script type="text/javascript">

            function OnClientClose(sender, eventArgs) {


                if (eventArgs.get_argument() == 'true')
                    $find("<%= RadAjaxManager.GetCurrent(Page).ClientID %>").ajaxRequest("Reload");

                else
                    return;
            }

        </script>
        </telerik:RadCodeBlock>
<telerik:RadGrid ID="RadGrid1" GridLines="None" runat="server" AllowAutomaticDeletes="True" AllowSorting="False"
            AllowAutomaticInserts="True" PageSize="10" AllowAutomaticUpdates="True" AllowMultiRowEdit="False"
            AllowPaging="False" DataSourceID="DataSource1" OnItemUpdated="RadGrid1_ItemUpdated"
            AllowFilteringByColumn="False" Skin ="Windows7" OnItemDeleted="RadGrid1_ItemDeleted" OnNeedDataSource="RadGrid1_NeedDataSource" >
            <PagerStyle Mode="NextPrevAndNumeric" />
            <MasterTableView Width="100%" CommandItemDisplay="TopAndBottom" DataKeyNames=""
                DataSourceID="DataSource1" HorizontalAlign="NotSet" EditMode="InPlace" AutoGenerateColumns="False">
                <CommandItemSettings ExportToPdfText="Export to PDF" ShowAddNewRecordButton="false" />
                <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">
                </RowIndicatorColumn>
                <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">
                </ExpandCollapseColumn>
                <Columns>
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                        <ItemStyle CssClass="MyImageButton" />
                    </telerik:GridEditCommandColumn>               
                    <telerik:GridBoundColumn DataField="Description" FilterControlAltText="Filter Description column" HeaderText="Description" SortExpression="Description" UniqueName="Description" ReadOnly="true">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Value" FilterControlAltText="Filter Value column" HeaderText="Value" SortExpression="Value" UniqueName="Value">
                    </telerik:GridBoundColumn>
                    <telerik:GridCheckBoxColumn DataField="IsActive" DataType="System.Boolean" FilterControlAltText="Filter IsActive column" HeaderText="IsActive" SortExpression="IsActive" UniqueName="IsActive" ReadOnly="true">
                    </telerik:GridCheckBoxColumn>
 
                </Columns>
                <EditFormSettings>
                   <FormTableItemStyle Wrap="False"></FormTableItemStyle>
                    <FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle>
                    <FormMainTableStyle GridLines="None" CellSpacing="0" CellPadding="3" BackColor="White"
                        Width="100%" />
                    <FormTableStyle CellSpacing="0" CellPadding="2" Height="110px" BackColor="White" />
                    <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle>
                    <EditColumn ButtonType="ImageButton"
                        UniqueName="EditCommandColumn1" CancelText="Cancel edit">
                    </EditColumn>
                    <FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle>
                </EditFormSettings>
            </MasterTableView>
            <FilterMenu EnableImageSprites="True">
            </FilterMenu>
        </telerik:RadGrid>
        <telerik:RadWindowManager ID="RadWindowManager1" runat="server"></telerik:RadWindowManager>
    </telerik:radajaxpanel>
        <asp:sqldatasource selectcommand="SELECT Description, Value, IsActive FROM AppConfiguration" connectionstring="<%$ ConnectionStrings:SludgeBillingSystem %>" providername="System.Data.SqlClient" id="DataSource1" runat="server"></asp:sqldatasource>
            </asp:Content>

----- code behind
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;

/// <summary>
/// Summary description for defaultcs
/// </summary>
public class defaultcs : System.Web.UI.Page
{
    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("CustomerID") as GridTextBoxColumnEditor;
                editor.TextBoxControl.Enabled = false;
            }
        }
    }
    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);
        }
    }
}

public partial class RadGridAutomaticCrudOperations : System.Web.UI.Page
{
    private string gridMessage = null;

    protected void RadGrid1_DataBound(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(gridMessage))
        {
            DisplayMessage(gridMessage);
        }
    }

    protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
    {
        if (e.Exception != null)
        {
            e.KeepInEditMode = true;
            e.ExceptionHandled = true;
            SetMessage("Update failed. Reason: " + e.Exception.Message);
        }
        else
        {
            SetMessage("Item updated!");
        }
    }

    protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
    {
        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            SetMessage("Insert failed! Reason: " + e.Exception.Message);
        }
        else
        {
            SetMessage("New product is inserted!");
        }
    }

    protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
    {
        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            SetMessage("Delete failed! Reason: " + e.Exception.Message);
        }
        else
        {
            SetMessage("Item deleted!");
        }
    }

    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;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //    RadGrid.

    }

    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {

    }
}

2 Answers, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 1
answered on 14 Aug 2014, 08:37 PM
One more point.

The datasource is clearly working, because the data is getting pulled in from the data source.
0
Princy
Top achievements
Rank 1
answered on 18 Aug 2014, 04:25 AM
Hi Kevin,

The datas are not getting saved because you haven't  written the UpdateCommand and added its Parameters. Use a uniqueId to identify the rows for Update. Please make the modification and let me know if the issue is solved.

ASPX:
<asp:SqlDataSource ID="DataSource1" runat="server"  ConnectionString="<%$ ConnectionStrings:SludgeBillingSystem %>"
SelectCommand="SELECT Description, Value , IsActive FROM AppConfiguration"
UpdateCommand="UPDATE [AppConfiguration] SET [Description] = @Description, [Value] = @Value, [IsActive] = @IsActive WHERE [ID] = @ID">   
        <UpdateParameters>
            <asp:Parameter Name="Description" Type="String"></asp:Parameter>
            <asp:Parameter Name="Value" Type="Int32"></asp:Parameter>
            <asp:Parameter Name="IsActive" Type="Boolean"></asp:Parameter>        
        </UpdateParameters>
    </asp:SqlDataSource>

Thanks,
Princy
Tags
Grid
Asked by
Kevin
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 1
Princy
Top achievements
Rank 1
Share this question
or