I hope this is an easy question to answer. I have a RadGrid with various bound columns, one of which is a GridDropDownColumn. I'm binding the column declaritively to a SQLDataSource as follows:
GridDropDownColumn:
When the RadGrid loads, I get what looks like text in the place of the GridDropDownColumn, and the text is what would be the first item in the bound dropdownlist.
I want the text to read the actual value that comes from the database. How do I make that happen?
GridDropDownColumn:
<
telerik:RadGrid
runat
=
"server"
ID
=
"rgUserMapping"
AutoGenerateColumns
=
"false"
AllowPaging
=
"true"
PageSize
=
"20"
>
...
<%-- MasterTableView Settings --%>
<
MasterTableView
AllowSorting
=
"true"
Caption
=
""
Summary
=
""
AllowFilteringByColumn
=
"true"
ItemStyle-VerticalAlign
=
"Top"
DataKeyNames
=
"UserMappingID"
CommandItemDisplay
=
"Bottom"
EditMode
=
"PopUp"
>
...
<%-- Columns --%>
<
Columns
>
...
<
telerik:GridDropDownColumn
DataField
=
"Component"
HeaderText
=
"Component"
FilterControlWidth
=
"50"
AutoPostBackOnFilter
=
"true"
CurrentFilterFunction
=
"Contains"
DataSourceID
=
"dsComponentDataSource"
ListTextField
=
"Title"
ListValueField
=
"ID"
DropDownControlType
=
"RadComboBox"
/>
...
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
When the RadGrid loads, I get what looks like text in the place of the GridDropDownColumn, and the text is what would be the first item in the bound dropdownlist.
I want the text to read the actual value that comes from the database. How do I make that happen?
5 Answers, 1 is accepted
0
Princy
Top achievements
Rank 2
answered on 29 Oct 2010, 05:39 AM
Hello Ryan,
In browser mode, GridDropDownColumn looks and behaves like a standard GridBoundColumn. When in edit mode it displays a drop-down control for each edited cell in the column. If you want to display the dropdown box in normal mode, then you need to use DropDownList inside GridTemplateColumn.
In order to display corresponding value for each item in DropDownColumn, you have to set the ListValueField property to the name of the field in the lookup table that supplies the value for the field specified by the DataField property.
Sample code below,
Mark-Up:
You can also refer the "GridDropDownColumn" section in the following documentation for more details on this.
Column types
Thanks,
Princy.
In browser mode, GridDropDownColumn looks and behaves like a standard GridBoundColumn. When in edit mode it displays a drop-down control for each edited cell in the column. If you want to display the dropdown box in normal mode, then you need to use DropDownList inside GridTemplateColumn.
In order to display corresponding value for each item in DropDownColumn, you have to set the ListValueField property to the name of the field in the lookup table that supplies the value for the field specified by the DataField property.
Sample code below,
Mark-Up:
<
telerik:GridDropDownColumn
DataField
=
"EmployeeID"
HeaderText
=
"Component"
FilterControlWidth
=
"50"
AutoPostBackOnFilter
=
"true"
CurrentFilterFunction
=
"Contains"
DataSourceID
=
"dsComponentDataSource"
ListTextField
=
"FirstName"
ListValueField
=
"EmployeeID"
DropDownControlType
=
"RadComboBox"
/>
You can also refer the "GridDropDownColumn" section in the following documentation for more details on this.
Column types
Thanks,
Princy.
0
Ryan
Top achievements
Rank 1
answered on 29 Oct 2010, 05:39 PM
Thank you for your response. I've also tried the GridTemplateColumn approach, and am unable to achieve the desired result. Please see my definition of the GridTemplate Column blow and let me know if something's off.
I obviously need a way to determine what the selected index of the dropdownlist should be, but am unaware of a property that I can set to determine that.
<
telerik:GridTemplateColumn
>
<
ItemTemplate
>
<
telerik:RadComboBox
runat
=
"server"
ID
=
"ddlComponent"
DataSourceID
=
"dsComponentDataSource"
DataTextField
=
"Title"
DataValueField
=
"ID"
/>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
I obviously need a way to determine what the selected index of the dropdownlist should be, but am unaware of a property that I can set to determine that.
0
Princy
Top achievements
Rank 2
answered on 01 Nov 2010, 06:27 AM
Hello Ryan,
You can use the SelectedValue property of RadComboBox to achieve this.Here is the example:
Thanks,
Princy.
You can use the SelectedValue property of RadComboBox to achieve this.Here is the example:
MarkUp:
<
telerik:GridTemplateColumn
>
<
ItemTemplate
>
<
telerik:RadComboBox
runat
=
"server"
ID
=
"ddlComponent"
DataSourceID
=
"dsComponentDataSource"
DataTextField
=
"Title"
DataValueField
=
"ID"
SelectedValue='<%# Bind("ID") %>' />
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
Thanks,
Princy.
0
Ryan
Top achievements
Rank 1
answered on 01 Nov 2010, 01:41 PM
Thanks. I've considered that before, but Visual Studio intellisense didn't permit SelectedValue in the RadComboBox. After reading your post, I decided to enter it anyway, and I got the following error: "The 'SelectedValue' property cannot be set declaritively".
My column is defined as follows:
Any other suggestions?
My column is defined as follows:
<
telerik:GridTemplateColumn
DataField
=
"Component"
HeaderText
=
"Component"
FilterControlWidth
=
"50"
AutoPostBackOnFilter
=
"true"
CurrentFilterFunction
=
"Contains"
>
<
ItemTemplate
>
<
telerik:RadComboBox
runat
=
"server"
ID
=
"ddlComponent"
DataSourceID
=
"dsComponentDataSource"
DataTextField
=
"Title"
DataValueField
=
"ID"
SelectedValue
=
"<%#Bind("
ID")%>" />
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
Any other suggestions?
0
Ryan
Top achievements
Rank 1
answered on 01 Nov 2010, 03:03 PM
I figured it out. I didn't even need to use a TemplateColumn. See the code below:
Markup:
Code Behind:
Thanks!
Markup:
<
telerik:GridDropDownColumn
UniqueName
=
"Component"
DataField
=
"Component"
HeaderText
=
"Component"
FilterControlWidth
=
"50"
AutoPostBackOnFilter
=
"true"
CurrentFilterFunction
=
"Contains"
DataSourceID
=
"dsComponentDataSource"
ListTextField
=
"Title"
ListValueField
=
"ID"
DropDownControlType
=
"RadComboBox"
/>
Code Behind:
Protected Sub rgUserMapping_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgUserMapping.ItemDataBound
'If the item is a standard DataItem
If (TypeOf e.Item Is GridDataItem) Then
'Get the item
Dim objDataItem As GridDataItem = DirectCast(e.Item, GridDataItem)
'Assign the text to the proper component
Dim ltrComponent As Literal = DirectCast(objDataItem("Component").Controls(0), Literal)
ltrComponent.Text = objDataItem.DataItem("Component")
End If
'If the item is an EditFormItem and it is currently in edit mode
If ((TypeOf e.Item Is GridEditFormItem) AndAlso (e.Item.IsInEditMode)) Then
'Get the item
Dim objEditedItem As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
'Select the correct component in the drop down
Dim ddlComponent As RadComboBox = DirectCast(objEditedItem("Component").Controls(0), RadComboBox)
ddlComponent.SelectedIndex = ddlComponent.FindItemIndexByText(objEditedItem.DataItem("Component"))
End If
End Sub
Thanks!