I have built a RadGrid with a MultiColumn ComboBox nested in an EditItemTemplate column This is the ASP code for the ComboBox. I based my design from the Telerik example from this Demo-Site
<EditItemTemplate> <telerik:RadComboBox RenderMode="Lightweight" runat="server" ID="RadComboBox1" EnableLoadOnDemand="True" DataTextField="FullName" OnItemsRequested="RadComboBox1_ItemsRequested" DataValueField="EID" AutoPostBack="true" HighlightTemplatedItems="true" Height="140px" Width="220px" DropDownWidth="420px" OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged"> <HeaderTemplate> <ul> <li class="col1">EID</li> <li class="col2">FullName</li> </ul> </HeaderTemplate> </telerik:RadComboBox> </EditItemTemplate>
And this is the C# code behind for the Combo
protected void RadComboBox1_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e) { string sql = "SELECT [EID], [FullName] from LU_Employees WHERE FullName LIKE @FullName + '%'"; SqlDataAdapter adapter = new SqlDataAdapter(sql, ConfigurationManager.ConnectionStrings["DatabaseLatentFingerpints"].ConnectionString); adapter.SelectCommand.Parameters.AddWithValue("@FullName", e.Text); DataTable dt = new DataTable(); adapter.Fill(dt); RadComboBox comboBox = (RadComboBox)sender; // Clear the default Item that has been re-created from ViewState at this point. comboBox.Items.Clear(); foreach (DataRow row in dt.Rows) { RadComboBoxItem item = new RadComboBoxItem(); item.Text = row["FullName"].ToString(); item.Value = row["EID"].ToString(); item.Attributes.Add("FullName", row["EID"].ToString()); comboBox.Items.Add(item); item.DataBind(); } }
The comboBox works but I now need to solve the problem of showing the Selected Item when I am editing the record. Right now when I click the edit button the ComboBox is blank. I read the Telerik information at This link and here is the code I used for the OnItemDataBoundHandler method;
protected void OnItemDataBoundHandler(object sender, GridItemEventArgs e) { if (e.Item.IsInEditMode) { GridEditableItem item = (GridEditableItem)e.Item; if (!(e.Item is IGridInsertItem)) { RadComboBox combo = (RadComboBox)item.FindControl("RadComboBox1"); RadComboBoxItem preselectedItem = new RadComboBoxItem(); preselectedItem.Text = item["FullName"].Text; preselectedItem.Value = item["EID"].Text; combo.Items.Insert(0, preselectedItem); combo.SelectedIndex = 0; } } }
When I run the app and click the edit link to to edit the record I get an error message When the code line "preselectedItem.Text = item["FullName"].Text;" runs. I have taken a screen shot of the error and have attached for your review
Can someone please tell me what I have done wrong with this code?