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

Update DataTable without using DataSource

6 Answers 256 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michal
Top achievements
Rank 1
Michal asked on 11 Jul 2008, 01:48 AM
Hello,

I have the following problem: I have a module in my project which return DataTable by property (ie <object name>.Data is of DataTable type). Then I bind this to the RadGrid. Everything works fine to the moment when I'm trying to update the DataTable. In the beginning, I was able to get to edit mode but pressing Update didn't do the job (ie the row was still in edit mode). Now after some changes I'm not able even to get to edit mode. I'm using RadNumericTextBox in template fields. Version of RadControls is 2008.1 619. This my code:

[aspx]

<!-- I use master page that is why I decided to use RadAjaxManagerProxy -->
<telerik:RadAjaxManagerProxy ID="RadAjaxManagerProxy1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="MarketShareRadGrid">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="MarketShareRadGrid" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManagerProxy>

...

<!-- This is my RadGrid -->
<telerik:RadGrid ID="MarketShareRadGrid" AllowAutomaticUpdates="true" AllowAutomaticDeletes="false"
            Skin="Vista" Width="97%" AllowSorting="False" AutoGenerateColumns="false" GridLines="None"
            runat="server" ShowFooter="True" AllowMultiRowSelection="True" AllowMultiRowEdit="True"
            HorizontalAlign="NotSet" OnUpdateCommand="MarketShareRadGrid_UpdateCommand">
            <MasterTableView Width="100%" GridLines="None" CommandItemDisplay="Top" DataKeyNames="Intervention"
                EditMode="InPlace" HorizontalAlign="NotSet" AllowAutomaticInserts="True">
                <CommandItemTemplate>
                    <div style="padding: 0 5px;">
                        Custom command item template&nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:LinkButton ID="btnEditSelected" runat="server" CommandName="EditSelected" Visible='<%# MarketShareRadGrid.EditIndexes.Count == 0 %>'><img style="border:0px;vertical-align:middle;" alt="" src="../../DataEditing/Img/Edit.gif" />Edit selected</asp:LinkButton>&nbsp;&nbsp;
                        <asp:LinkButton ID="btnUpdateEdited" runat="server" CommandName="UpdateEdited" Visible='<%# MarketShareRadGrid.EditIndexes.Count > 0 %>'><img style="border:0px;vertical-align:middle;" alt="" src="../../DataEditing/Img/Update.gif" />Update</asp:LinkButton>&nbsp;&nbsp;
                        <asp:LinkButton ID="btnCancel" runat="server" CommandName="CancelAll" Visible='<%# MarketShareRadGrid.EditIndexes.Count > 0 || MarketShareRadGrid.MasterTableView.IsItemInserted %>'><img style="border:0px;vertical-align:middle;" alt="" src="../../DataEditing/Img/Cancel.gif" />Cancel editing</asp:LinkButton>&nbsp;&nbsp;
                        <asp:LinkButton ID="LinkButton4" runat="server" CommandName="RebindGrid"><img style="border:0px;vertical-align:middle;" alt="" src="../../DataEditing/Img/Refresh.gif" />Refresh table</asp:LinkButton>
                    </div>
                </CommandItemTemplate>
                <Columns>
                    <telerik:GridBoundColumn HeaderText="Intervention" UniqueName="Intervention" DataField="Intervention" ReadOnly="true" />
                    <telerik:GridTemplateColumn HeaderText="Year 1" UniqueName="Year 1" DataField="Year 1">
                        <ItemTemplate>
                            <telerik:RadNumericTextBox ID="Year1Item" runat="server" Skin="Outlook" MinValue="0" MaxValue="100" Type="Percent" Enabled="false"
                                Value='<%# (float)Eval("Year 1") %>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <telerik:RadNumericTextBox ID="Year1Edit" runat="server" Skin="Outlook" ShowSpinButtons="true" MinValue="0" MaxValue="100" Type="Percent"
                                Value='<%# (float)DataBinder.Eval(Container.DataItem, "Year 1") %>' />
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
            <ClientSettings>
                <Selecting AllowRowSelect="True" EnableDragToSelectRows="True" />
            </ClientSettings>
        </telerik:RadGrid>

[cs]

public partial class Screen1 : System.Web.UI.Page
{  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Calculations.MarketShare marketShareObject = new MarketShare(-1); // I'm creating the object of my module closed in DLL
            MarketShareRadGrid.DataSource = marketShareObject.Data; // The property Data returns DataTable
        }
    }

protected void MarketShareRadGrid_UpdateCommand(object source, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;

        int rowNumber = editedItem.ItemIndex; // I'm checking which row is edited

        double year1 = (double)(editedItem["Year 1"].Controls[1] as RadNumericTextBox).Value; // I'm taking a value which user provided

        try
        {
            ((DataTable)((RadGrid)source).DataSource).Rows[rowNumber]["Year 1"] = year1; // I'm writing this value to my DataTable
        }
        catch (Exception ex)
        {
            MarketShareRadGrid.Controls.Add(new LiteralControl("Unable to update value!"));
            e.Canceled = true;
        }
    }
}

Thank you in advance for your help.

6 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 11 Jul 2008, 06:26 AM
Hello Michal,

If you assign DataSource for the grid in NeedDataSource you will able to get to edit mode - otherwise you should handle EditCommand, assign DataSource for the grid an call DataBind().

Greetings,
Vlad
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Michal
Top achievements
Rank 1
answered on 11 Jul 2008, 12:36 PM
Hello,

I modified my code in that way and I can't still get of edit mode after update (ItemUpdated event is not raised). My code looks like this now:

[cs]

protected void MarketShareRadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        if (Session["MarketShareTable"] != null)
        {
            ((RadGrid)source).DataSource = (DataTable)Session["MarketShareTable"];
        }
    }

protected void MarketShareRadGrid_UpdateCommand(object source, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;

        int rowNumber = editedItem.ItemIndex;

        double year1 = (double)(editedItem["Year 1"].Controls[1] as RadNumericTextBox).Value;

        ((DataTable)Session["MarketShareTable"]).Rows[rowNumber].BeginEdit();
        try
        {
            //((DataTable)((RadGrid)source).DataSource).Rows[rowNumber]["Year 1"] = year1;
            ((DataTable)Session["MarketShareTable"]).Rows[rowNumber]["Year 1"] = year1;
            ((DataTable)Session["MarketShareTable"]).Rows[rowNumber].EndEdit();
            MarketShareRadGrid.DataBind();
        }
        catch (Exception ex)
        {
            MarketShareRadGrid.Controls.Add(new LiteralControl("Unable to update value!"));
            e.Canceled = true;
        }
    }

// This event is not raised
protected void MarketShareRadGrid_ItemUpdated(object source, GridUpdatedEventArgs e)
    {
        e.KeepInEditMode = false;
    }

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Calculations.MarketShare marketShareObject = new MarketShare(-1);
            Session["MarketShareTable"] = marketShareObject.Data; // returns DataTable
        }
    }

Thank you for your help in advance.
0
Vlad
Telerik team
answered on 11 Jul 2008, 12:56 PM
Hi Michal,

ItemUpdated is applicable only when the grid is bound using DataSourceID. You can set e.Item.Edit = false in UpdateCommand to handle your case.

Regards,
Vlad
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Michal
Top achievements
Rank 1
answered on 11 Jul 2008, 01:33 PM
I modified my code in that way and it still doesn't work. What is more the first RadNumericTextBox in edit mode is slighty lower than it should be.

protected void MarketShareRadGrid_UpdateCommand(object source, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;

        int rowNumber = editedItem.ItemIndex;

        double year1 = (double)(editedItem["Year 1"].Controls[1] as RadNumericTextBox).Value;

        ((DataTable)Session["MarketShareTable"]).Rows[rowNumber].BeginEdit();
        try
        {
            //((DataTable)((RadGrid)source).DataSource).Rows[rowNumber]["Year 1"] = year1;
            ((DataTable)Session["MarketShareTable"]).Rows[rowNumber]["Year 1"] = year1;
            ((DataTable)Session["MarketShareTable"]).Rows[rowNumber].EndEdit();
            MarketShareRadGrid.DataBind();
            editedItem.Edit = false;
            editedItem.Selected = false;

        }
        catch (Exception ex)
        {
            MarketShareRadGrid.Controls.Add(new LiteralControl("Unable to update value!"));
            e.Canceled = true;
        }
    }
0
Michal
Top achievements
Rank 1
answered on 11 Jul 2008, 04:39 PM
This is an exception which occurs. It is caused by e.Item.Edit = false in OnUpdateCommand event.

 Only items with IsInEditMode set to true can be updated
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Telerik.Web.UI.GridException: Only items with IsInEditMode set to true can be updated

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[GridException: Only items with IsInEditMode set to true can be updated]
   Telerik.Web.UI.GridTableView.PerformUpdate(GridEditableItem editedItem, Boolean suppressRebind) +311
   Telerik.Web.UI.GridCommandEventArgs.ExecuteCommand(Object source) +1817
   Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e) +130
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
   Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +36
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
   Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +112
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +115
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +132
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +177
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746
0
Sebastian
Telerik team
answered on 14 Jul 2008, 12:56 PM
Hi Michal,

When you use NeedDataSource binding and handle update/insert operations manually, you should disable the automatic editing operations by setting AllowAutomaticUpdates/AllowAutomaticInserts to false (since they are not meaningful in such scenario), otherwise the edit/insert form will remain open.

Thus you will not need to set explicitly e.Item.Edit to false and the forms should be closed after the edit/insert action is completed.
 
Best regards,
Stephen
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Grid
Asked by
Michal
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Michal
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or