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

FormTemplate - Set gender values in EF Databound combobox

4 Answers 189 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 25 Apr 2013, 12:02 PM
Hi,

I'm hoping someone can explain how to do what seems like a relatively straightforward task.  

I'm using radgrid to display a list of users bound to an Entitydatasource control .  To insert or edit the users I am using a FormTemplate for the edit/insert.

One of the fields for the user data is gender.  Rather than create a lookup table for this I wanted to create a combobox containing only the values (Not Selected, Male, Female).   The default value displayed on the combo-box for an insert should be Not Selected.  The default value
for an edit should be the underlying value from the db that has previously been selected.

If I bind the combo directly to the field I am given all the values that are currently in the table for that field (ie. Male, Male, Female, Not Selected).

Instead of this I want a combobox with only the above values, while it is also bound to the underlying entitydatasource and thus can have its selectedvalue set.

I've been through as many posts and articles as I can find that seem to be relevant but I still don't seem to be solving the problem some examples below.

http://www.telerik.com/help/aspnet-ajax/grid-with-combobox.html
http://www.telerik.com/help/aspnet-ajax/grid-operations-with-dropdownlist-in-edititemtemplate.html

If I implement the recommendation from the above article I do end up with a combo-box limited to the selected values but as it is not bound to the database I cannot retrieve the correct value on edit nor persist any selected values back the database using EF. 

It seems I'm missing something obvious here so please feel free to point it out to me as any help on this would be welcome.




4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 25 Apr 2013, 12:34 PM
Hi,

Please take a look into the following code snippet.

C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
 
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
        {
            RadComboBox combo = (RadComboBox)e.Item.FindControl("RadComboBox1");
            combo.SelectedIndex = 0; //index of the item you need to select in insert mode
        }
        else
        {
            RadComboBox combo = (RadComboBox)e.Item.FindControl("RadComboBox1");
            combo.SelectedValue = (string)DataBinder.Eval(e.Item.DataItem, "YourDataField").ToString();
        }
    }
}

Thanks,
Shinu.
0
Michael
Top achievements
Rank 1
answered on 25 Apr 2013, 02:24 PM
Thanks for taking the time to look at this Shinu.

I'm just going to post up some code and images that may make things clearer than my initial post.

I have cut the aspx file down to just the gender field to make it easier to view.

As you can see the entitydatasource is bound to the field. 

DataSourceID="UserEntityDataSource"  DataTextField="GENDER" DataValueField="GENDER"

If I use this with the below code behind file I get what you see in the attached picture.
The dropdown is set based on the current records value but as you can see it displays

Male
Female
Female

This is a direct bind from the gender field in the table and is not what I require.

Instead it should be

Not Selected (or empty string)
Male
Female

I want  the above static set of values that limit the user options while also allowing the selected index to be set.

<EditFormSettings EditFormType="Template">
    <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
    <FormTemplate>
         
        <div id="editUsersLeft">
            <fieldset>
                     
                    
            <p>
                <label for="cmbGender">Gender</label>
                <telerik:RadComboBox ID="cmbGender"
                                     DataSourceID="UserEntityDataSource" runat="server" DataTextField="GENDER" DataValueField="GENDER" >
                    <Items>
                        <telerik:RadComboBoxItem runat="server" />
                        <telerik:RadComboBoxItem runat="server" Text="Male" Value="Male" />
                        <telerik:RadComboBoxItem runat="server" Text="Female" Value="Female" />
                    </Items>
                </telerik:RadComboBox>
 
            </p>
        </div>
 
        <div id="editUsersFooter">
            <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                        runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>
            </asp:Button
            <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                        CommandName="Cancel"></asp:Button
        </div>
 
    </FormTemplate>
</EditFormSettings>

With Shinu's suggested ItemDataBound this gives me a databound column which shows all the values in the field which is not what I want.

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
 
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
        {
            RadComboBox combo = (RadComboBox)e.Item.FindControl("cmbGender");
            combo.SelectedIndex = 0; //index of the item you need to select in insert mode
        }
        else
        {
            RadComboBox combo = (RadComboBox)e.Item.FindControl("cmbGender");
            combo.SelectedValue = (string)DataBinder.Eval(e.Item.DataItem, "GENDER").ToString();
        }
    }
}

The alternate approach I tried is this which will display the dropdown with correct values but as it is not databound it will not insert or update the gender values.

<EditFormSettings EditFormType="Template">
    <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
    <FormTemplate>
         
        <div id="editUsersLeft">
            <fieldset>
                     
                    
            <p>
                <label for="cmbGender">Gender</label>
                <telerik:RadComboBox ID="cmbGender"
                                     runat="server" >
 
                </telerik:RadComboBox>
 
            </p>
        </div>
 
        <div id="editUsersFooter">
            <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                        runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>
            </asp:Button
            <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                        CommandName="Cancel"></asp:Button
        </div>
 
    </FormTemplate>
</EditFormSettings>


void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
    {
        //Attempts to set combo values
        GridEditFormItem editFormItem = (GridEditFormItem)e.Item;
        RadComboBox combo = (RadComboBox)editFormItem.FindControl("cmbGender");
        string[] genderOptions = { "", "Male", "Female" };
        combo.DataSource = genderOptions;
        combo.DataBind();
        combo.SelectedIndex = e.Item.ItemIndex;
    }
}

If I attempt to use the above I get this error which points out that I am attempting to rebind the already databound combox as per below.

"Both DataSource and DataSourceID are defined on 'cmbGender'.  Remove one definition."

So the issue is I can't have both a data-bound combo-box displaying a static set of values while linking to the databound field.  If its unbound I can't set the selected index correctly, if its bound it will display the "wrong" dropdown values (not the static set I have set in the above RadGrid1_ItemDataBound).

I'm just not sure what the resolution is based on this except that I'm sure its fairly obvious I'm just not seeing it right now

Thanks again for any help other posters can offer.
0
Michael
Top achievements
Rank 1
answered on 26 Apr 2013, 12:32 PM
I can't seem to get this to work the way I want it to so I am implementing a lookup table for this instead.  It will get a little annoying having to implement look-up tables for what should be static data but I can't find the way around this currently.  I'm still interested in an answer but for the moment I'll move ahead with the look-up table instead.

Thanks.
0
Antonio Stoilkov
Telerik team
answered on 30 Apr 2013, 07:52 AM
Hello Michael,

The reason for the experienced behavior is that the UserEntityDataSource returns three user records which have Male, Female, Female values for their Gender type. In order to resolve your issue you could create a separate table which holds the gender types and bind the RadComboBox to data source which selects the newly created table. Note that the same behavior you are experiencing with the duplicated data could be observed with ASP.NET DropDownList.

Greetings,
Antonio Stoilkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Michael
Top achievements
Rank 1
Antonio Stoilkov
Telerik team
Share this question
or