Thursday 13 December 2007

Problems Using Telerik Controls for inserts

I had an issue today using the Telerik RadGrid control (Version 5.0) to do inserts (edits work fine). If you don't initialize the fields in your control properly, you will get the following error:

DataBinding: 'Telerik.WebControls.GridInsertionObject' does not contain a property with the name 'Person_Responsible_Id'.

I don't think this is an ideal situation - the control should be smart enough to initialize its own fields - but the way around this is described here on the Telerik site:

http://www.telerik.com/help/aspnet/grid/?grdInsertingValuesUserControlFormTemplate.html



   1:  protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)

   2:  {

   3:             GridEditableItem editedItem = e.Item as GridEditableItem;

   4:             UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);

   5:   

   6:             //Create new row in the DataSource

   7:             DataRow newRow = this.Employees.NewRow();

   8:   

   9:             //Insert new values

  10:             Hashtable newValues = new Hashtable();

  11:   

  12:             newValues["Country"] = (userControl.FindControl("TextBox7") as TextBox).Text;

  13:             newValues["City"] = (userControl.FindControl("TextBox8") as TextBox).Text;

  14:             newValues["Region"] = (userControl.FindControl("TextBox9") as TextBox).Text;

  15:             newValues["HomePhone"] = (userControl.FindControl("TextBox10") as TextBox).Text;

  16:             newValues["BirthDate"] = (userControl.FindControl("TextBox11") as TextBox).Text;

  17:             newValues["TitleOfCourtesy"] = (userControl.FindControl("ddlTOC") as DropDownList).SelectedItem.Value;

  18:   

  19:             newValues["Notes"] = (userControl.FindControl("TextBox1") as TextBox).Text;

  20:             newValues["Address"] = (userControl.FindControl("TextBox6") as TextBox).Text;

  21:             newValues["FirstName"] = (userControl.FindControl("TextBox2") as TextBox).Text;

  22:             newValues["LastName"] = (userControl.FindControl("TextBox3") as TextBox).Text;

  23:             newValues["HireDate"] = (userControl.FindControl("Textbox5") as TextBox).Text;

  24:             newValues["Title"] = (userControl.FindControl("TextBox4") as TextBox).Text;

  25:   

  26:             //make sure that unique primary key value is generated for the inserted row

  27:             newValues["EmployeeID"] = (int)this.Employees.Rows[this.Employees.Rows.Count - 1]["EmployeeID"] + 1;

  28:             try

  29:             {

  30:                 foreach (DictionaryEntry entry in newValues)

  31:                 {

  32:                     newRow[(string)entry.Key] = entry.Value;

  33:                 }

  34:                 this.Employees.Rows.Add(newRow);

  35:                 this.Employees.AcceptChanges();

  36:             }

  37:             catch (Exception ex)

  38:             {

  39:                 RadGrid1.Controls.Add(new LiteralControl("Unable to update/insert Employees. Reason: " + ex.Message));

  40:                 e.Canceled = true;

  41:             }

  42:  }

1 comment:

Unknown said...

Hello my friend...
I´m from Brazil and I am having the same problem in telerik you ... You managed to resolve this?
Waiting ...
Later,
Have a nice day!