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

Extract Values from GridEditFormItem

1 Answer 501 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Hemika
Top achievements
Rank 1
Hemika asked on 30 May 2013, 11:14 AM
Hello, 
How to extract values from a GridEditFormItem from server side.
Here is my code.

ASP Markup :
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" CellSpacing="0"
    Skin="Transparent" GridLines="None" PageSize="10" AllowCustomPaging="True" AutoGenerateColumns="False"
    AllowSorting="True" OnItemDataBound="RadGrid1_ItemDataBound1" OnRowDrop="RadGrid1_OnRowDrop"
    AllowAutomaticUpdates="true" OnUpdateCommand="RadGrid1_UpdateCommand">
    <PagerStyle Mode="NumericPages" />
    <ClientSettings AllowRowsDragDrop="True">
        <Selecting AllowRowSelect="true" />
        <ClientEvents OnRowDropping="rowDropping" OnRowDblClick="RowDblClick" OnCommand="GridCommand" />
        <Resizing AllowColumnResize="true" />
    </ClientSettings>
    <MasterTableView AllowCustomPaging="False" AutoGenerateColumns="false" Caption="<%$Resources:BrandixResources, PWI_UnplannedItemsGrid_Title%>"
        Visible="True" DataKeyNames="ID, Effort" AllowNaturalSort="false" ClientDataKeyNames="Subject, Description, Effort">
        <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
        <SortExpressions>
            <telerik:GridSortExpression FieldName="DueDateTime" SortOrder="Ascending" />
        </SortExpressions>
        <Columns>
            <telerik:GridBoundColumn UniqueName="ID" Visible="false" DataField="ID" ReadOnly="True">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="<%$Resources: Resources, Subject%>"
                DataField="Subject" UniqueName="Subject" ReadOnly="True">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="<%$Resources: Resources,  DueDate%>"
                DataField="DueDateTime" DataFormatString="{0:dd/MM/yyyy} {0:hh}:{0:mm} {0:tt}"
                ReadOnly="True">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="<%$Resources: Resources, Effort%>"
                DataField="Effort">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="<%$Resources: Resources,  UnplannedHours%>"
                DataField="UnplannedHours" ReadOnly="True">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn HeaderText="<%$Resources: Resources,  DraftPlan%>"
                UniqueName="DraftPlan" SortExpression="Status" ReadOnly="True">
                <ItemTemplate>
                    <asp:Label ID="lblGridStatus" runat="server" />
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
        <EditFormSettings>
            <EditColumn UniqueName="EditCommandColumn1">
            </EditColumn>
        </EditFormSettings>
    </MasterTableView>
</telerik:RadGrid>

Javascript : 

var editedRow;
 
        function RowDblClick(sender, eventArgs) {
            editedRow = eventArgs.get_itemIndexHierarchical();
            $find("<%= RadGrid1.ClientID %>").get_masterTableView().editItem(editedRow);
        }
 
        function GridCommand(sender, args) {
            if (args.get_commandName() != "Edit") {
                editedRow = null;
            }
        }


C# code : 
Attempt 1 :
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
    GridEditableItem item = e.Item as GridEditableItem;
    Dictionary<string, string> values = new Dictionary<string, string>();
    item.ExtractValues(values);
    int id = Convert.ToInt32(item.GetDataKeyValue("ID"));
}

Attempt 2 : 
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
    GridEditableItem item = e.Item as GridEditableItem;
    Dictionary<string, string> values = new Dictionary<string, string>();
    e.Item.OwnerTableView.ExtractValuesFromItem(values, item);
}

When I double click a row, the edit form pops below the row. But when I change the value and click update. It is not getting the new value. Instead its getting the old value. Please tell me how to get the new value.

Img1 : The new value I entered,
Img2 : Value I get from the code.

Really need some help on this.

1 Answer, 1 is accepted

Sort by
0
Elliott
Top achievements
Rank 2
answered on 30 May 2013, 09:00 PM
is there a reason the columns don't all have a UniqueName?
to get the value of the key
cast the e.Item to whatever the object is for your edit, depending on the EditMode used (popup, user control,in place, etc)
the key field will be object.OwnerTableView.DataKeyValues(object.ItemIndex)("key field name")
the rest of fields can easily be accessed by casting object("unique name").Controls(0) to a TextBox for GridBoundColumns
for templated columns it will be Controls(1) - and cast to whatever the Edit object is
Tags
Grid
Asked by
Hemika
Top achievements
Rank 1
Answers by
Elliott
Top achievements
Rank 2
Share this question
or