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

Getting new values from Edit Mode

3 Answers 679 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mitch
Top achievements
Rank 1
Mitch asked on 07 Jan 2011, 10:25 PM
DISCLAIMER:  I am brand new to the Telerik ASP.NET AJAX toolkit.  So my apologies if these are truly "newbie" questions.

Environment: ASP.NET 3.5, Windows 7, IE 8, ASP.NET, Telerik ASP.NET AJAX Q3 2009 SP3


I have a RadGrid with a number of columns.
I have successfully bound the RadGrid to a DataTable in the C# code behind.  When the grid is displayed, the correct values are shown.

Note:  I am using stored procedures to populate my source DataTable.  In order to update the database properly, I have to manually execute multiple stored procedures.  So, it's my understanding that I can't use "Automatic" mode for this RadGrid.  (Am I correct on this?)

When I click the Edit link on one of the grid's row, the row is changed to allow me to enter new values. (FYI, I've used both in-place and EditForm editing.)

The problem is that once I click the Update link, the new values I typed in are not saved.  (My plan is to get the new values typed in, and then manually update the database via the various stored procs.)

I added an UpdateCommand event handler, but when I look at e.Item, it gives me the the old value, not the new value.

Please advise...

Thanks,

Mitch

 

protected void RadGridNextSteps_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
    GridDataItem editItem = e.Item as GridDataItem; // found the row
    TableCell cell = editItem["sDisplayText"]; // found the cell
    TextBox txt = cell.Controls[0] as TextBox; // found the control
    string itemValue = txt.Text; // found the control’s value
    // Problem: itemValue is old value, not new value.
}

 

3 Answers, 1 is accepted

Sort by
0
Mike Nogen
Top achievements
Rank 1
answered on 07 Jan 2011, 11:48 PM
Hello!

The thing you are doing wrong here (I think) Is that you access the GridDataItem. You should use the GridEditableItem instead. Here is an example where I pick the changed RadPicker value in the UpdateCommande.

protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
        {
            GridEditableItem item = (GridEditableItem)e.Item;
            String id = item.GetDataKeyValue("ID").ToString();
              
            RadColorPicker picker = (RadColorPicker)e.Item.FindControl("RadColorPicker1");
            DAL.CalendarDataContext db = new DAL.CalendarDataContext();
            Table<DAL.EventColor> eventColors = db.GetTable<DAL.EventColor>();
  
            var q = (from c in eventColors
                    where c.ID.Equals(int.Parse(id))
                    select c).FirstOrDefault();
  
            q.Color = ColorTranslator.ToHtml(picker.SelectedColor);
              
            db.SubmitChanges();
        }






0
Mitch
Top achievements
Rank 1
answered on 10 Jan 2011, 10:46 PM
Hi Mike,

Thank you very much for your reply!

I tried using GridEditableItem, and followed the example in http://www.telerik.com/help/aspnet-ajax/grdupdatinginplaceandeditforms.html , "Using ExtractValuesFromItem".  When I got to the line

changedRows[0][(string)entry.Key] = entry.Value;

entry.Value lists the old value, not the new value I entered.

It's my understanding that if you click Edit, change a field and then click Update, the new value shown will only reflect what's currently in the DataSource (e.g., database or DataSet), correct?

I've gotten another test application to successfully change a field in a row of a RadGrid, but I used "automatic" mode (i.e., I followed the RadGrid Tutorial and implemented an Update SQL statement).  Can I still use this "automatic" mode if I want one of the fields to to be a Dropdown list?  I tried, but the problem above (entry.Value doesn't have the newly entered value) prevents me from updating the database properly in the UpdateCommand event handler.

Thanks!
0
Tsvetoslav
Telerik team
answered on 14 Jan 2011, 07:28 AM
Hello Mitch,

Straight to your questions.

1. You cannot use automatic operations if you are updating the underlying data-store through stored procedures.

2. In order to get the new values in the UpdateCommand you should use the ExtractValues method, as you have correctly noted passing to it a Hashtable:

Hashtable newValues = new Hashtable().
  
((GridEditableItem)e.Item).ExtractValues(newValues);
  
// now newValues will be populated with a dictionary containing field name/value pairs for each column with the exception of the readonly ones


3. You can use a GridDropDownColumn with automatic operations or a template column with a RadComboBox in its EditItemTemplate. Do note in the latter approach that in order for the new values to be extracted from the drop-down control, you need to set a binding expression for the SelectedValue property of the combo box inspite of its being unavailable to IntelliSense. For example:
<telerik:GridTemplateColumn DataField="CategoryID" UniqueName="Category">
    <ItemTemplate>
        <asp:Literal ID="lContactName" runat="server" Text='<%#Eval("CategoryName") %>'></asp:Literal>
    </ItemTemplate>
    <EditItemTemplate>
        <telerik:RadComboBox ID="rcmbCotnactName" DataSourceID="ddcDataSource" SelectedValue='<%#Bind("ContactName" %>'
            DataValueField="CategoryID" DataTextField="CategoryName" runat="server">
        </telerik:RadComboBox>
    </EditItemTemplate>
</telerik:GridTemplateColumn>


Hope it helps.

Regards,
Tsvetoslav
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Mitch
Top achievements
Rank 1
Answers by
Mike Nogen
Top achievements
Rank 1
Mitch
Top achievements
Rank 1
Tsvetoslav
Telerik team
Share this question
or