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

Add New Row, then filtering lookup columns based on another lookup column value

1 Answer 47 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 12 Aug 2010, 05:26 PM
Hi,

I saw a post or sample code somewhere on the Telerik site on how to filter template columns which are lookup columns, based on another lookup column value, I now cannot find it to reference it.  But it is not working and I cannot see why.

Basically, my grid is for questions, under each question there are four lookup columns.  The values in lookups 2, 3 and 4 should be filtered according to the value selected in lookup column 1.  This is working fine but only for editing existing questions.  When creating a new question the code is firing the events but other lookups are never filtered.  This is what I am doing:

Any ideas or help greatly appreciated.

Thanks

protected void RadGridQuestions_ItemCreated(object sender, GridItemEventArgs e)
  {
      if ((e.Item is GridEditFormItem || e.Item is GridEditFormInsertItem) && e.Item.IsInEditMode)
      {
          if (e.Item.OwnerTableView.Columns.FindByUniqueNameSafe("Test_Type_Id") == null) return;
           
          RadComboBox ddlTestType = (e.Item as GridEditableItem)["Test_Type_Id"].Controls[0] as RadComboBox;
 
          // Attach SelectedIndexChanged event for the dropdownlist control
          ddlTestType.AutoPostBack = true;
          ddlTestType.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(TestTypeOfQuestion_SelectedIndexChanged);
          TestTypeOfQuestion_SelectedIndexChanged(ddlSubObjective, null);
          ddlTestType.PreRender += new EventHandler(ddlTestType_PreRender);
      }
  }
 
  private void ddlTestType_PreRender(object sender, EventArgs e)
  {
      TestTypeOfQuestion_SelectedIndexChanged(sender, null);
  }
 
  /// <summary>
  /// Changed test type so update objective and sub objective only relevant for that exam type
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void TestTypeOfQuestion_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
  {
      // First reference the edited grid item through the NamingContainer attribute
      GridEditableItem editedItem = (sender as RadComboBox).NamingContainer as GridEditableItem;
 
      // The dropdown list will be the first control in the Controls collection of the corresponding cell
      string strTestTypeId = (editedItem["Test_Type_Id"].Controls[0] as RadComboBox).SelectedValue;
      if (string.IsNullOrEmpty(strTestTypeId))
          return;
 
      RadComboBox ddlKeyElement = editedItem["Question_Category_Id"].Controls[0] as RadComboBox;
      SqlDataSourceQuestionCatLookupEdit.SelectParameters["Test_Type_Id"].DefaultValue = strTestTypeId;
      ddlKeyElement.ClearSelection();
      ddlKeyElement.DataSource = SqlDataSourceQuestionCatLookupEdit;
      ddlKeyElement.DataBind();
 
      RadComboBox ddlObjective = editedItem["Objective_Id"].Controls[0] as RadComboBox;
      SqlDataSourceObjectiveLookupEdit.SelectParameters["Test_Type_Id"].DefaultValue = strTestTypeId;
      ddlObjective.ClearSelection();
      ddlObjective.DataSource = SqlDataSourceObjectiveLookupEdit;
      ddlObjective.DataBind();
 
      RadComboBox ddlSubObjective = editedItem["Sub_Objective_Id"].Controls[0] as RadComboBox;
      SqlDataSourceSubObjectiveLookupEdit.SelectParameters["Test_Type_Id"].DefaultValue = strTestTypeId;
      ddlSubObjective.ClearSelection();
      ddlSubObjective.DataSource = SqlDataSourceSubObjectiveLookupEdit;
      ddlSubObjective.DataBind();
  }

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 18 Aug 2010, 07:41 AM
Hi Paul,

Do you have a selected value for the combo under the field Test_Type_ID when you insert a new item? I can see you return from the TestTypeOfQuestion_SelectedIndexChanged event handler if strTestTypeId is an empty string, requiring your combo to always have a selected value, even in insert mode:

private void TestTypeOfQuestion_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
  // First reference the edited grid item through the NamingContainer attribute
  GridEditableItem editedItem = (sender as RadComboBox).NamingContainer as GridEditableItem;
 
  // The dropdown list will be the first control in the Controls collection of the corresponding cell
  string strTestTypeId = (editedItem["Test_Type_Id"].Controls[0] as RadComboBox).SelectedValue;
  if (string.IsNullOrEmpty(strTestTypeId))
      return;
  //...
}


Regards,
Veli
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Paul
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or