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

DropDownColumn with defined set of values

9 Answers 153 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 20 Jan 2011, 02:50 PM
How do I define a dropdown column for a RadGrid where I just have a predefined set of values (ideally just in the aspx page).

eg. I have a dataField = "Type" and it only the following possible values
"General" = 0
"Special" = 1
"Other" = 2

So I want a ddl that shows the text of those three options with the value linked to the Type field in my underlying datasource

9 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 21 Jan 2011, 06:53 AM
Hello Paul,

One option is you can use GridTemplateColumn with DriopDownList in EdiiItemTemplate. Sample code is given below.

ASPX:
<telerik:GridTemplateColumn >
      <ItemTemplate>
          <%#Eval("Type") %>
      </ItemTemplate>
      <EditItemTemplate>
          <asp:DropDownList ID="DropDownList1" runat="server">
              <asp:ListItem Text="General" Value="0"></asp:ListItem>
              <asp:ListItem Text="Special" Value="1"></asp:ListItem>
              <asp:ListItem Text="Other" Value="2">
              </asp:ListItem>
          </asp:DropDownList>
      </EditItemTemplate>
  </telerik:GridTemplateColumn>

Thanks,
Princy.
0
Paul
Top achievements
Rank 1
answered on 21 Jan 2011, 10:40 AM
Hi. Thanks that is exactly what I wanted. I had only found examples that constructed the ddl either from datasources or from code behind. Thanks for your help its greatly appreciated.
0
Paul
Top achievements
Rank 1
answered on 21 Jan 2011, 10:48 AM
Actually a slight problem. I may be able to figure it out but if you can provide a quick answer that would be great.

I've just tried your code and it works. However, the insert and update queries do not now work. How do I bind the ddl to the Type datafield for insert and update?

Thanks
0
Paul
Top achievements
Rank 1
answered on 21 Jan 2011, 11:21 AM
OK now I've had a couple of minutes to look at the solution its not really what I need and I'm not sure whether it will do what I want. I was hoping that the dropdowncolumn could support it. That would then provide a neat integrated solution. However using a gridtemplatecolumn presents a number of problems:

- The itemTemplate uses an Eval. I would need to put condition code in there for it to actually display the text representation of the numeric codes (if value=0 "General"... etc.)
- I end up with multiple templates (item, edit...) and repeat the numeric to text info in each. Thus for maintenance there is a potential to edit one template and miss another
- I have the problem (complexity) of binding the asp:dropdownlist to the field in my underlying grid datasource.
- I think I end up avoiding the problems by adding an additional (unwelcome) table to my database just to contain the text/numeric value pairs for the ddl and I can then use a dropdowncolumn which will provide an integrated solution

Is there no way to build a dropdowncolumn from either code behind (without a datasource) or from the aspx page?
0
Princy
Top achievements
Rank 2
answered on 24 Jan 2011, 10:14 AM
Hello Paul,

You can customize GridDropDownColumn to achieve this. I hope the following documentation will help you. Please go through it.
Customize/Configure GridDropDownColumn

Thanks,
Princy.
0
Paul
Top achievements
Rank 1
answered on 24 Jan 2011, 12:16 PM
Hi Princy

Thanks for the help but the link doesn't really help me. The GridDropDown is fine if you have a data source but I still haven't found any info that explains how to construct it within the aspx page (or whether that is even possible). If it isn't possible then I think that it should be considered as an future enhancement.

In the grid demo there is a page the shows the different column types. One example of the dropdown column is to list the Title for a person (Mr, Mrs, Miss....). This is generated from employees table but that obviously only works if there is at least one person already in the table with each of the required forms of Title. It seems wasteful to have a table dedicated to just Titles and much better to simply define the options within the control definition on the aspx page.

In the link that you provided there is some code that sets the datasource to an array (in the code behind) but no real explanation of this. It feels like there should be a way to use the datasource, data and list properties to configure the ddl but the documentation isn't there to explain how to do it (or I haven't found it or I am just not understanding it)
0
Veli
Telerik team
answered on 27 Jan 2011, 06:47 PM
Hi Paul,

The RadGrid Customize/Configure GridDropDownColumn help topic actually explains how to customize the dropdown list / combo in the edited items. You need to use RadGrid's ItemDataBound event, find the combo/ddl through the editable item's edit manager and re-assign its data source. Alternatively, you can just clear the items and add new ones without databinding the combo/ddl to a data source. This is explained in the Customizing the options for GridDropDownColumn on editing section of the help topic (towards the bottom of the page).

If you do not have a data source at all, and all you need is just a couple of values bound to the combo, you really need to use a GridTemplateColumn, as the above discussed dropdown column is designed to work with a separate data source. You can have a combo with values in the EditItemTemplate that you can bind to the edited item either through a binding expression (such as Eval() or Bind()), or programmatically through RadGrid's ItemDataBound event (as explained in the above help topic similar to the GridDropDownColumn configuration). The same event handler can be used to set the text value of the cells in regular view mode, if you do not want to use a binding expression in the ItemTemplate of the column:

protected void RadGrid1_ItemDataBound (object sender, GridItemEventArgs e)
{
    if  (e.Item is GridDataItem)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        dataItem["dropDownColumn"].Text = DataBinder.Eval(dataItem.DataItem, "DropDownFieldName").ToString();
    }
     
    if  (e.Item is GridEditableItemItem && e.Item.IsInEditMode)
    {
        GridEditableItem editItem = (GridEditableItem)e.Item;
        RadComboBox combo = editItem.FindControl("RadComboBoxIDHere");
        combo.SelectedValue = DataBinder.Eval(dataItem.DataItem, "DropDownFieldName").ToString();
    }
}

Greetings,
Veli
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Paul
Top achievements
Rank 1
answered on 28 Jan 2011, 12:00 AM
Hi Veli. Thank you for the reply which is greatly appreciated. However, there are two parts to my post/questions:

1) I would like to find the best solution to the problem

2) I am really also asking whether the present controls should/could be enhanced if they cannot do what I am looking for

I tend to take the approach that simple solutions are often the best ones. If I use the example of a field for "Title" (Mr, Mrs, Miss, Dr...) I can achieve this in a very simple neat solution in a grid with a ddl IF I have a table that defines the permitted values of Title. I can then use that table (via a datasource) as the datasource for the text/value fields of the ddl and the datafield then being linked to a field in the grid's underlying datasource. However, I don't really want to create tables for every trivial set of defined values (and importantly I want to reduce the database access so I want to avoid unnecessary tables/datasources).

The ideal would therefore be to have the option to not have a datasource for the dll and in the aspx page define the ddl with its items collection (ie have <items>...</items> in the aspx page).

In my example, say I have a table "Contacts" that has a field "Title". I could define Title as an Int or a string with the permitted values being:

"Mr"==0, "Mrs"==2, "Miss"==3....

Using TemplateColumns is not really a good solution and has several problems associated with it. Also dealing with this in code behind in the itemDataBound event add complexity. I'd just like a simple solution.
0
Veli
Telerik team
answered on 28 Jan 2011, 11:02 AM
I don't believe it could be made simpler than a GridTemplateColumn. How would you go about this requirement with a standard ASP.NET GridView control?

Veli
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Paul
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Paul
Top achievements
Rank 1
Veli
Telerik team
Share this question
or