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

Template Column ComboBox Dropdown in Editform?

2 Answers 266 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mahesh
Top achievements
Rank 1
Mahesh asked on 18 Feb 2009, 11:58 AM
Hello,
I am trying to get a dropdown in Edit form as a template.
Firstly I have a RadGrid in that there is a dropdown.I binded that successfully.
Upto here there is no problem.But now I have a Edit template form which opens on click of update button.
Here I have two textbox and a combo as a template.I want to show all values in Edit combo that are present in grids combo.I can fetch that again from a database.But the selected value should be same as Grid's combo's selected value.
How to do that.I tried by findcontrol method.I am getting correct combo from edit form.
But when I bind that to database I am not getting.
Thanks in advance,
-Mahesh

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 18 Feb 2009, 12:45 PM
Hello Mahesh,

You can try out the following code to access the dropdown in the EditItemTemplate of a TemplateColumn and then set its selectedindex as shown below:
aspx:
  <telerik:GridTemplateColumn> 
            <ItemTemplate> 
                <asp:DropDownList ID="DropDown1" DataSourceID="SqlDataSource1" DataTextField="ProductName" DataValueField="ProductName" runat="server"
                </asp:DropDownList> 
            </ItemTemplate> 
            <EditItemTemplate> 
                <asp:DropDownList ID="DropDown2" runat="server"
                </asp:DropDownList> 
            </EditItemTemplate> 
 </telerik:GridTemplateColumn> 

cs:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditableItem && e.Item.IsInEditMode ) 
          {             
             GridEditableItem editItem = e.Item as GridEditableItem; 
             DropDownList dropdown = (DropDownList)editItem.FindControl("DropDown2"); 
             dropdown.DataSourceID="SqlDataSource1"
             dropdown.DataTextField = "ProductName"
             dropdown.SelectedIndex = editItem.ItemIndex; 
          } 
    } 

Thanks
Princy.
0
Mahesh
Top achievements
Rank 1
answered on 18 Feb 2009, 12:59 PM
Thanks Princy.But I am getting a Error :

Add Event Listener is null or not an object



Here is my code.

<EditFormSettings EditFormType="Template">

<EditColumn UniqueName="EditCommandColumn1"></EditColumn>

 

                    <FormStyle BackColor="#ECFED8" />

                    <FormTemplate>

                        <table id="Table1" cellspacing="1" cellpadding="1" width="250" border="0">

                            <tr>

                                <td>

                                </td>

                                <td>

                                </td>

                            </tr>

                            <tr>

                                <td>

                                    Employee_Name:</td>

                                <td>

                                    <asp:TextBox ID="TextBox10" Text='<%# Bind( "Employee_Name") %>' runat="server">

                                    </asp:TextBox></td>

                            </tr>

                            <tr>

                                <td>

                                    Designation:</td>

                                <td>

                                    <asp:TextBox ID="TextBox11" Text='<%# Bind( "Employee_Designation") %>' runat="server">

                                    </asp:TextBox></td>

                            </tr>

                            <tr><telerik:RadComboBox  ID="RadComboBox3" runat="server"></telerik:RadComboBox>

                                   </tr></td></td>

                            </tr>

                        </table>

                        <table style="width: 100%">

                            <tr>

                                <td align="right" colspan="2">

                                    <asp:Button ID="Button1" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'

                                        runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>

                                    </asp:Button>&nbsp;

                                    <asp:Button ID="Button2" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel">

                                    </asp:Button>

                                </td>

                            </tr>

                        </table>

                    </FormTemplate>

                </EditFormSettings>  

 

 

  protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)

 {

 

     if (e.Item is Telerik.Web.UI.GridDataItem)

     {

 

         Telerik.Web.UI.RadComboBox cmb = (Telerik.Web.UI.RadComboBox)e.Item.FindControl("RadComboBox1");

         SqlConnection = new SqlConnection("Server=ma;DataBase=Test;User=sa;Password=");

         SqlDataAdapter da = new SqlDataAdapter("Select Employee_Id from Employee_Master", SqlConnection);

         DataTable dt1 = new DataTable();

         da.Fill(dt1);

         //string[] options = { "OptionA", "OptionB", "OptionC" };

         cmb.DataSource = dt1;

         cmb.DataMember = "Employee_Id";

         cmb.DataTextField = "Employee_Id";

         //cmb.SelectedIndex = i;

         cmb.DataBind();

         cmb.SelectedItem.Text = dt1.Rows[i]["Employee_Id"].ToString();

         i++;

 

     }

     if (e.Item is GridEditFormItem && e.Item.IsInEditMode)

     {                        

         //GridEditFormItem editFormItem = e.Item as GridEditFormItem;

         //GridDataItem parentItem = editFormItem.ParentItem;

         //Telerik.Web.UI.RadComboBox box = editFormItem.FindControl("RadComboBox3") as Telerik.Web.UI.RadComboBox;

         SqlConnection = new SqlConnection("Server=ma;DataBase=Test;User=sa;Password=");

         SqlDataAdapter da1 = new SqlDataAdapter("Select Employee_Id from Employee_Master", SqlConnection);

         DataTable dt1 = new DataTable();

         da1.Fill(dt1);

         //box.DataTextField = parentItem["Employee_Name"].Text;

         //box.DataSource = dt1;

         //box.DataMember = "Employee_Id";

         //box.DataTextField = "Employee_Id";

         GridEditFormItem formItem = (GridEditFormItem)e.Item;

         RadComboBox dropdown = (RadComboBox)formItem.FindControl("RadComboBox3");

         dropdown.DataSource= dt1;

         dropdown.DataTextField = "Employee_Id";

         dropdown.DataValueField = "Employee_Id";

         dropdown.SelectedValue = DataBinder.Eval(formItem.DataItem, "Employee_Id").ToString();

Tags
Grid
Asked by
Mahesh
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Mahesh
Top achievements
Rank 1
Share this question
or