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

Cant get templatecolumn to show the text of the selected item of a radcombobox in an edititemtemplate

5 Answers 28 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ben
Top achievements
Rank 1
Ben asked on 21 Dec 2016, 09:25 PM

I cant get the item template to reflect the text of the selected item in the radcombobox that is in the edititemtemplate of my programmatically created gridtemplatecolumn

Code for creating the template column:

 

01.Dim tempDB As New SqlDataSource
02.Dim templateColumnName As String = col.Caption
03.Dim templateColumn As New GridTemplateColumn()
04. 
05.templateColumn.DataField = col.Caption
06.templateColumn.ItemTemplate = New LCDisp(templateColumnName, ddQuery, DiConnectionString)
07.templateColumn.EditItemTemplate = New DDEdit(templateColumnName, ddQuery, DiConnectionString)
08.templateColumn.HeaderText = templateColumnName
09. 
10.templateColumn.UniqueName = col.Caption
11. 
12.grdReport.Columns.Add(templateColumn)

 

Code for LCDisp template:

01.Private Class LCDisp
02.    Implements ITemplate
03.    Protected lblCont As Label
04.    Private colname As String
05.    Private DSQuery As String
06.    Private DiConnectionString As String
07. 
08.    Public Sub New(ByVal cName As String, ByVal query As String, ByVal connString As String)
09.        colname = cName
10.        DSQuery = query
11.        DiConnectionString = connString
12.    End Sub
13. 
14.    Public Sub InstantiateIn(container As UI.Control) Implements ITemplate.InstantiateIn
15.        lblCont = New Label
16.        lblCont.ID = "lControl" + colname
17.        AddHandler lblCont.DataBinding, AddressOf litCont_DataBinding
18.        container.Controls.Add(lblCont)
19.    End Sub
20. 
21.    Sub litCont_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
22.        Dim l As Label = DirectCast(sender, Label)
23.        Dim container As GridDataItem = DirectCast(l.NamingContainer, GridDataItem)
24.        l.Text = (DirectCast(container.DataItem, DataRowView))(colname).ToString()
25.    End Sub
26.End Class

 

Code for DDEdit:

01.Private Class DDEdit
02.    Implements ITemplate
03.    Protected DropDown As RadComboBox
04.    Private colname As String
05.    Private DSQuery As String
06.    Private DiConnectionString As String
07.    Private MyContainerDataItem As GridDataItem
08. 
09.    Public Sub New(ByVal cName As String, ByVal query As String, ByVal connString As String)
10.        colname = cName
11.        DSQuery = query
12.        DiConnectionString = connString
13.    End Sub
14. 
15.    Public Sub InstantiateIn(container As UI.Control) Implements ITemplate.InstantiateIn
16.        DropDown = New RadComboBox
17. 
18.        Dim dv2 As New DataView
19.        Dim oQuery As New cWjCoQuery
20. 
21.        oQuery.Load(DSQuery, ConfigurationManager.ConnectionStrings(DiConnectionString).ConnectionString)
22. 
23.        Dim tempDB As New SqlDataSource
24.        tempDB.ID = colname + "DB"
25. 
26.        tempDB.SelectCommand = oQuery.SQLQuery
27.        tempDB.ConnectionString = ConfigurationManager.ConnectionStrings(DiConnectionString).ConnectionString
28. 
29.        dv2 = tempDB.Select(DataSourceSelectArguments.Empty)
30.        DropDown.DataSource = tempDB
31.        If dv2.Table.Columns.Count > 1 Then
32.            DropDown.DataTextField = dv2.Table.Columns.Item(0).Caption
33.            DropDown.DataValueField = dv2.Table.Columns.Item(1).Caption
34.        Else
35.            DropDown.DataTextField = dv2.Table.Columns.Item(0).Caption
36.            DropDown.DataValueField = dv2.Table.Columns.Item(0).Caption
37.        End If
38. 
39.        DropDown.DataBind()
40. 
41.        MyContainerDataItem = TryCast(container.Parent, GridDataItem)
42.        If MyContainerDataItem Is Nothing Then
43.            MyContainerDataItem = TryCast(container.Parent.Parent, GridDataItem)
44.        End If
45.        If MyContainerDataItem IsNot Nothing Then
46.            Dim tmpSelVal = MyContainerDataItem.GetDataKeyValue(colname).ToString
47.            If Trim(tmpSelVal) <> "" Then
48.                DropDown.SelectedIndex = DropDown.FindItemIndexByValue(CInt(tmpSelVal))
49.            End If
50.        End If
51. 
52.        container.Controls.Add(DropDown)
53.    End Sub
54.End Class

5 Answers, 1 is accepted

Sort by
0
Vasil
Telerik team
answered on 26 Dec 2016, 07:18 AM
Hi Ben,

I see that you are setting the text at this line:
Text = (DirectCast(container.DataItem, DataRowView))(colname).ToString()
It looks correct, isn't the text applied?

The edit and the item template are not linked by any way. Edit template is used when the item is in edit move, and the item template when the item is in view mode.
Generally when you Update your record, the new values will be used after the grid rebinds, and they will show up in the ItemTemplate after the new data is fetched from the server.

Regards,
Vasil
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Ben
Top achievements
Rank 1
answered on 03 Jan 2017, 05:27 PM

Hi Vasil,

My table is in batch edit mode, so the item created event isn't fired server side when I edit the row. The text is applied. However, I get the value and not the associated text. When I change the value using the drop down, the text value of the new selection is shown.

 

For example, I have a database of fruits and customer orders.

My fruit table is as follows

[[Fruit, fCode],

[Apple,1],

[Banana,2],

[Orange,3]]

 

My customer table may look like this

[[Customer, fOrder, Quantity],

[Vasil, 2, 100],

[Ben, 1, 50]]

 

If I want to display my customer table and apply the template dropdown column to the fOrder column, the displayed table looks like this:

[[Customer, fOrder, Quantity],
[Vasil, 2, 100],
[Ben, 1, 50]]

 

The desired result when the table first displays would be:

[[Customer, fOrder, Quantity],
[Vasil, Banana, 100],
[Ben, Apple, 50]]

 

When I change the value of the drop down, it will show the fruits name instead of its fCode value, but once saved, it once again displays the code instead of the fruit.

 

I hope this clarifies what I am trying to do.

0
Vasil
Telerik team
answered on 06 Jan 2017, 02:12 PM
Hello Ben,

If you are using the Batch edit, and you try to use dropdown for some of the columns, please examine the source code of this demo.

The code of the demo is working, and you could adapt it to your data.
http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/batch-editing/defaultvb.aspx?show-source=true

Regards,
Vasil
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Ben
Top achievements
Rank 1
answered on 07 Jan 2017, 12:22 AM
Unfortunately, The template column is being created programmatically so the demo doesn't help me all that much.
0
Vasil
Telerik team
answered on 11 Jan 2017, 12:19 PM
Hello,

The client side logic will not change if the control is created declaratively or programmatically.

The difference between the demo and your code (I am guessing here, since I don't see what you are binding the grid to). Is that in the demo the main table, contains both ID's and the Names. Here is the select statement used for populating the data:
SelectCommand="SELECT [ProductID], [ProductName], [Products].[CategoryID], [Categories].[CategoryName] as CategoryName, [UnitPrice], [Discontinued], [QuantityPerUnit], [UnitsInStock] FROM [Products] JOIN Categories ON Products.CategoryID=Categories.CategoryID"

Now the grid knows both GategoryID and CategoryName for each of your records.

From your explanation I got that when you select the item from the combo it looks okay, but after save it still display the code. This is because the grid itself don't know the data that combobox have.

In the demo it is used:
<%# Eval("CategoryName") %>

In your code, you are using:
Text = (DirectCast(container.DataItem, DataRowView))(colname).ToString();

Where I believe that container.DataItem, DataRowView))(colname) gives you the ID instead of the Name.

Regards,
Vasil
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Ben
Top achievements
Rank 1
Answers by
Vasil
Telerik team
Ben
Top achievements
Rank 1
Share this question
or