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

ComboBox in Grid Column bind to Enum

6 Answers 575 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Igor T
Top achievements
Rank 1
Igor T asked on 23 Mar 2010, 03:52 PM
Hi,
I'm looking for a way to bind to Enum and found this article how it's done for Silverlight control:
http://blogs.telerik.com/valerihristov/posts/09-06-12/binding_a_combobox_to_enum_values_with_radcontrols_for_silverlight.aspx

I'm following below example for my project, and would like to get the exact functionality as "Contact Title" column, except binding to enum as the source.
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/filtertemplate/defaultcs.aspx

I wasn't able to find an example for asp.net radcombobox. Can you please point me in the right direction.

Thank you!

6 Answers, 1 is accepted

Sort by
0
Kalina
Telerik team
answered on 25 Mar 2010, 09:13 AM
Hi Igor T,

Here is one simple example for databinding a RadComboBox to enum:

public enum Names
{
    name1,
    name2
}
 
public enum Test
{
    a = 1,
    b = 2
}
 
protected void Page_Load(object sender, EventArgs e)
{
    RadComboBox1.DataSource = Enum.GetNames(typeof(Names));
    RadComboBox1.DataBind();
 
    foreach (int index in Enum.GetValues(typeof(Test)))
    {
        RadComboBoxItem item = new RadComboBoxItem();
        item.Value = index.ToString();
        item.Text = Enum.GetName(typeof(Test), index);
        RadComboBox2.Items.Add(item);
    }
}

<form id="form1" runat="server">
    <div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <telerik:RadComboBox ID="RadComboBox1" runat="server">
        </telerik:RadComboBox>
        <telerik:RadComboBox ID="RadComboBox2" runat="server">
        </telerik:RadComboBox>
    </div>
</form>

Please let me know if this was helpful.

All the best,
Kalina
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.
0
Igor T
Top achievements
Rank 1
answered on 31 Mar 2010, 03:52 PM
Hi,
thanks for your reply. I'd like to reference the combox box which is a filter for the column as below. How would I reference that one in the codebehind? I was able to do it through ObjectDataSource, however I'd like to complete it by using your approach.  Please see code snippet below...Thanks!

<telerik:GridBoundColumn HeaderText="Type" UniqueName="Type"
            AllowSorting="true" DataField="Type">
            <FilterTemplate>
                            <telerik:RadComboBox ID="RadComboBoxType" AppendDataBoundItems="true" SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("Type").CurrentFilterValue %>'
                                runat="server" OnClientSelectedIndexChanged="TypeIndexChanged">
                                <Items>
                                    <telerik:RadComboBoxItem Text="All" />
                                </Items>
                            </telerik:RadComboBox>
                            <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">

                                <script type="text/javascript">
                                    function TypeIndexChanged(sender, args) {
                                        var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
                                        tableView.filter("Type", args.get_item().get_value(), "EqualTo");

                                    }
                                </script>

                            </telerik:RadScriptBlock>
            </FilterTemplate>
       </telerik:GridBoundColumn>

0
Kalina
Telerik team
answered on 05 Apr 2010, 02:03 PM
Hello Igor T,

I suppose that you use this online demo in your implementation.
That is why the sample page that I made for you is based on it.

I will suggest you handle OnLoad event of the RadComboBox and populate the control with data:

public enum Test
{
    Germany,
    Mexico
}
protected void OnLoadHandler(object sender, EventArgs e)
{
    RadComboBox combo = (RadComboBox)sender;
    combo.Items.Add(new RadComboBoxItem("", ""));
    foreach (int index in Enum.GetValues(typeof(Test)))
    {
        RadComboBoxItem item = new RadComboBoxItem();
        item.Value = Enum.GetName(typeof(Test), index);
        item.Text = Enum.GetName(typeof(Test), index);
        combo.Items.Add(item);
    }
 
}

<telerik:GridTemplateColumn DataField="Country" HeaderText="Country" UniqueName="Country"
    HeaderStyle-Width="200px" SortExpression="Country">
    <FilterTemplate>
        <telerik:RadComboBox ID="RadComboBoxCountry" Height="100px" AppendDataBoundItems="true"
            SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("Country").CurrentFilterValue %>'
            runat="server" OnLoad="OnLoadHandler" OnClientSelectedIndexChanged="CountryIndexChanged">
        </telerik:RadComboBox>
        <telerik:RadScriptBlock ID="RadScriptBlock3" runat="server">
 
            <script type="text/javascript">
                function CountryIndexChanged(sender, args) {
                    console.log(args.get_item().get_value());
                    console.log(args.get_item().get_text());
                    var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
                    tableView.filter("Country", args.get_item().get_text(), "EqualTo");
                }
            </script>
 
        </telerik:RadScriptBlock>
    </FilterTemplate>
    <ItemTemplate>
        <%# Eval("Country") %>
    </ItemTemplate>
</telerik:GridTemplateColumn>


Regards,
Kalina
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.
0
Igor T
Top achievements
Rank 1
answered on 12 Apr 2010, 02:50 PM
Hi,
I'm using your code and get this error when page loads(see below, System.ArgumentOutOfRangeException: Selection out of range Parameter name).

If I remove  SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("Country").CurrentFilterValue %>', page loads...

The filtering works at this point without selectedvalue shown since I took it out.

Also I'm also using radslider in another column. When I change index for "Country" grid correctly filters, however I get "Unknown runtime error" js error where looks like ClearSelection occurs when I start moving slider.
},_clearSelection:function(){if(this._document.selection&&this._document.selection.empty){this._document.selection.empty()

Thanks for your help.

System.ArgumentOutOfRangeException: Selection out of range Parameter name: value at Telerik.Web.UI.RadComboBox.PerformDataBinding(IEnumerable dataSource) at Telerik.Web.UI.RadComboBox.OnDataSourceViewSelectCallback(IEnumerable data) at Telerik.Web.UI.RadComboBox.OnDataBinding(EventArgs e) at Telerik.Web.UI.RadComboBox.PerformSelect() at System.Web.UI.Control.DataBindChildren() at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) at System.Web.UI.Control.DataBindChildren() at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) at Telerik.Web.UI.GridItem.SetupItem(Boolean dataBind, Object dataItem, GridColumn[] columns, ControlCollection rows) at Telerik.Web.UI.GridTableView.CreateFilteringItem(Boolean useDataSource, GridColumn[] copiedColumnSet, GridTHead thead) at Telerik.Web.UI.GridTableView.CreateControlHierarchy(Boolean useDataSource) at Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource) at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) at System.Web.UI.WebControls.DataBoundControl.PerformSelect() at Telerik.Web.UI.GridTableView.PerformSelect() at Telerik.Web.UI.GridTableView.DataBind() at Telerik.Web.UI.RadGrid.DataBind() at Telerik.Web.UI.RadGrid.AutoDataBind(GridRebindReason rebindReason) at Telerik.Web.UI.RadGrid.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
0
Kalina
Telerik team
answered on 13 Apr 2010, 08:57 AM
Hi Igor T,

Since on our side the code works properly, could you please explain with more details your implementation?What is different on your side? Could you please paste your code here?

Thank you in advance.

All the best,
Kalina
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.
0
Igor T
Top achievements
Rank 1
answered on 13 Apr 2010, 04:34 PM
Hi,
I will try to figure it out. If I'm still having trouble I will attach my code.

Thanks for all your help.

Igor
Tags
ComboBox
Asked by
Igor T
Top achievements
Rank 1
Answers by
Kalina
Telerik team
Igor T
Top achievements
Rank 1
Share this question
or