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

Picking up when an item is selected in the griddropdowncolumn

1 Answer 27 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kathy
Top achievements
Rank 1
Kathy asked on 29 Sep 2010, 09:38 PM
Here's what I'm trying to do:  I have a griddropdowncolumn with a data source attached.  I've enabled the EmptyListItemText.  What I would like is, if the datasource is empty, add another item to the griddropdowncolumn which would say "add an item".  When the user selects this text, it would bring up a popup edit form with a couple of fields.  The user would fill in those fields and do an insert.  This new value would then show up in the griddropdowncolumn.  I guess I would need to use the ItemDataBound event to add the "add the item" text.  But how do I determine that someone selected this text?  I guess I'm asking what event would get fired when someone picks a different item in the griddropdowncolumn.  Thanks for any help I can get.

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 30 Sep 2010, 07:55 AM
Hello Kathy,

Here is one sample code that I tried in my application to achieve the same requirement. In ItemDataBound event, add the "add the item" text and attach an 'SelectedIndexChanged' event to RadComboBox. In that event handler check with the selected item text and if its 'add the item' open a RadWindow to add new item to RadComboBox.  Also, modify the logic based on your requirement.

ASPX:
<telerik:RadWindow ID="RadWindow1" runat="server">
   <ContentTemplate>
      <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
      <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
   </ContentTemplate>
 </telerik:RadWindow>
 
<telerik:RadGrid ID="RadGrid1" OnItemDataBound="RadGrid1_ItemDataBound" >
  <MasterTableView  CommandItemDisplay="Top">
      <Columns>
         <telerik:GridEditCommandColumn>
         </telerik:GridEditCommandColumn>
         <telerik:GridDropDownColumn DataField="EmployeeID" DataSourceID="SqlDataSource2" HeaderText="Status" ListTextField="Status" ListValueField="Status" UniqueName="Status_ID">
         </telerik:GridDropDownColumn>
      </Columns>
   </MasterTableView>
 </telerik:RadGrid>

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
      if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
       {
           GridEditFormItem editItem = (GridEditFormItem)e.Item;
           RadComboBox drp = (RadComboBox)editItem["Status_ID"].Controls[0];
           if (drp.Items.Count <= 0)
           {
               RadComboBoxItem item = new RadComboBoxItem("Select","0");
               RadComboBoxItem item1 = new RadComboBoxItem("add the item", "1");
               drp.Items.Add(item);
               drp.Items.Add(item1);
           }
           drp.AutoPostBack = true;
           drp.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(drp_SelectedIndexChanged);
      }
   }
 private void drp_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
   {
       RadComboBox combo = (RadComboBox)o;
       if (combo.SelectedItem.Text == "add the item")
            RadWindow1.VisibleOnPageLoad = true; // open RadWindow for adding new item
   }
// adding new item to RadComboBox
protected void Button2_Click(object sender, EventArgs e)
   {
       Button btn = (Button)sender;
       GridEditFormItem editItem = (GridEditFormItem)RadGrid1.MasterTableView.GetItems(GridItemType.EditFormItem)[Convert.ToInt32(RadGrid1.EditIndexes[0])]; // get the edit form
       RadComboBox combo = (RadComboBox)editItem["Status_ID"].Controls[0];
       RadComboBoxItem item = new RadComboBoxItem(TextBox1.Text);
       combo.Items.Add(item);
       combo.DataBind();
       RadWindow1.VisibleOnPageLoad = false;
   }

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