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

Insert and Update Manually using code behind

1 Answer 754 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mohammed
Top achievements
Rank 1
Mohammed asked on 14 Dec 2013, 04:42 PM
I have problem inserting and updating Radgrid from code behind manually 
In Inserting I cannot get the data from grid using ExtractValuesFromItem
In updating I get the old data not the updated data using ExtractValuesFromItem also 
here's the source code

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="grdMain">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="grdMain"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
   <telerik:RadGrid ID="grdMain" runat="server" OnInsertCommand="grdMain_InsertCommand" OnUpdateCommand="grdMain_UpdateCommand" >
            <MasterTableView EditMode="InPlace" CommandItemDisplay="Top"  AutoGenerateColumns="False" >
            <Columns>
                 <telerik:GridBoundColumn  DataField="Id" DataType="System.Int32"
                   HeaderText="Id" Visible="true" UniqueName="Id" ReadOnly="true">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="MinValue"
 
DataType="System.Int32"
                    FilterControlAltText="Filter MinValue column"
 
HeaderText="MinValue"
                    SortExpression="MinValue" UniqueName="MinValue">
                      <ColumnValidationSettings
 
EnableRequiredFieldValidation="true" RequiredFieldValidator-ControlToValidate="">
                        <RequiredFieldValidator ForeColor="Red"
 
ErrorMessage="*"></RequiredFieldValidator>
                      </ColumnValidationSettings>
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="MaxValue"
 
DataType="System.Int32"
                    FilterControlAltText="Filter MaxValue column"
 
HeaderText="MaxValue"
                    SortExpression="MaxValue" UniqueName="MaxValue">
                   <ColumnValidationSettings
 
EnableRequiredFieldValidation="true">
                        <RequiredFieldValidator ForeColor="Red"
 
ErrorMessage="*"></RequiredFieldValidator>
                      </ColumnValidationSettings>
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="ResultDate"
 
DataType="System.DateTime"
                    FilterControlAltText="Filter ResultDate column"
 
HeaderText="ResultDate"
                    SortExpression="ResultDate" UniqueName="ResultDate">
                      <ColumnValidationSettings
 
EnableRequiredFieldValidation="true">
                        <RequiredFieldValidator ForeColor="Red"
 
ErrorMessage="*"></RequiredFieldValidator>
                      </ColumnValidationSettings>
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="ResultValue"
 
DataType="System.Int32"
                    FilterControlAltText="Filter ResultValue column"
 
HeaderText="ResultValue"
                    SortExpression="ResultValue" UniqueName="ResultValue">
                      <ColumnValidationSettings
 
EnableRequiredFieldValidation="true">
                        <RequiredFieldValidator ForeColor="Red"
 
ErrorMessage="*"></RequiredFieldValidator>
                      </ColumnValidationSettings>
                </telerik:GridBoundColumn>
                 <telerik:GridEditCommandColumn uniqueName="EditCommandColumn"></telerik:GridEditCommandColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>


protected void grdMain_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        AnalyzerResultBO _BO = new AnalyzerResultBO();  //Business object
        GridEditableItem editedItem = e.Item as GridEditableItem;
        Hashtable InputValues = new Hashtable();
        //The GridTableView will fill the values from all editable columns in the hash
          e.Item.OwnerTableView.ExtractValuesFromItem(InputValues, editedItem);
 
        _BO.MaxValue = int.Parse(InputValues["MaxValue"].ToString());
        _BO.MinValue = int.Parse(InputValues["MinValue"].ToString());
        _BO.ResultValue = int.Parse(InputValues["ResultValue"].ToString());
        _BO.ResultDate = InputValues["ResultDate"].ToString();
 
 
    }
 
    protected void grdMain_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        AnalyzerResultBO _BO = new AnalyzerResultBO(); //Business Object
        GridEditableItem editedItem = e.Item as GridEditableItem;
        Hashtable InputValues = new Hashtable();
        //The GridTableView will fill the values from all editable columns in the hash
        e.Item.OwnerTableView.ExtractValuesFromItem(InputValues, editedItem);
 
        _BO.MaxValue = int.Parse(InputValues["MaxValue"].ToString());
        _BO.MinValue = int.Parse(InputValues["MinValue"].ToString());
        _BO.ResultValue = int.Parse(InputValues["ResultValue"].ToString());
        _BO.ResultDate = InputValues["ResultDate"].ToString();
    }


1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 16 Dec 2013, 12:12 PM
Hi Mohammed,

I tried your code and it works fine at my end. Below is as sample code snippet for InPlace edit.

ASPX:
<telerik:RadGrid ID="grdMain" runat="server" OnInsertCommand="grdMain_InsertCommand" OnUpdateCommand="grdMain_UpdateCommand" AllowPaging="true">
    <MasterTableView EditMode="InPlace" CommandItemDisplay="Top" AutoGenerateColumns="False"
        DataKeyNames="OrderID">
        <Columns>
            <telerik:GridBoundColumn DataField="OrderID" HeaderText="OrderID" UniqueName="OrderID"
                ReadOnly="true">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="EmployeeID" HeaderText="EmployeeID" SortExpression="EmployeeID"
                UniqueName="EmployeeID">
            </telerik:GridBoundColumn>
            <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
            </telerik:GridEditCommandColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void grdMain_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{      
    GridEditableItem editedItem = e.Item as GridEditableItem;
    Hashtable InputValues = new Hashtable();      
    e.Item.OwnerTableView.ExtractValuesFromItem(InputValues, editedItem);      
    int eid = int.Parse(InputValues["EmployeeID"].ToString());
}
protected void grdMain_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
    GridEditableItem editedItem = e.Item as GridEditableItem;
    string id = editedItem.GetDataKeyValue("OrderID").ToString();
    Hashtable InputValues = new Hashtable();
    e.Item.OwnerTableView.ExtractValuesFromItem(InputValues, editedItem);
    int eid = int.Parse(InputValues["EmployeeID"].ToString());
}

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