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

Dynamic creation of DropDownColumn

3 Answers 69 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Johnathan
Top achievements
Rank 1
Johnathan asked on 23 Sep 2010, 02:35 PM
Hi

What I am try doing might not be possible or sensible.

I am building a dynamic grid page to edit / insert/ delete data from an unknown tablename with an unknown number of fields (except at render time).

I know that all of my field are one of two types, either a numeric or a string which is often entered multiple times.

I build the columns / insert / edit SQL dynamically and I have the basics working.
I want my "string" columns to appear in the insert / edit (inline) form as a combo box to allow them to type customer text or select from a list of existing items (populated as a distinct list of all previous data entered).

Problem 1: I have changed the BoundColumn to a griddropdown, but now on view mode of the grid the field values are empty (were fine with boundcolumn) but the combo appears on clicking insert.

Problem 2: Can I allow customtext in a griddropdowncolumn? if so how do I do this.

Problem 3: I can't work out how to populate the combo with the preset values. Is it possible to bind to a datareader in code or is my approach wrong?

Below is my code running in page_load. "Case 6" is the problem area. case 6 means that the column is a string and should use the dropdown.

Thanks in advance.

Johnathan

' build grid
Dim FieldListList As SqlDataReader = kpiSQL.getReader("select * from dbo.kpi_QuantityColumn where tableid=" & tableid.ToString)
While FieldListList.Read
    Select Case FieldListList("fielddataType")
        Case 6 ' is a string column
            Dim boundColumn As GridDropDownColumn
            boundColumn = New GridDropDownColumn()
            boundColumn.EmptyListItemText = "--type or choose--"
            ' allowcustomtext???
            boundColumn.UniqueName = FieldListList("FieldName")
            boundColumn.DataField = FieldListList("FieldName")
            boundColumn.DropDownControlType = GridDropDownColumnControlType.RadComboBox
            boundColumn.HeaderText = FieldListList("FieldName")
            boundColumn.HeaderStyle.Wrap = False
            Dim presetReader As SqlDataReader = kpiSQL.getReader("SELECT DISTINCT " & FieldListList("FieldName") & " FROM " & thisTable.dbTableName)
            ' ???? how do I bind the combo to presetReader? 
            RadGrid1.MasterTableView.Columns.Add(boundColumn)
        
        Case Else ' is a numeric column
            Dim boundColumn As GridNumericColumn
            boundColumn = New GridNumericColumn()
            RadGrid1.MasterTableView.Columns.Add(boundColumn)
            boundColumn.UniqueName = FieldListList("FieldName")
            boundColumn.DataField = FieldListList("FieldName")
            Dim prefix As String = ""
            If Not IsDBNull(FieldListList("prefix")) And FieldListList("FieldName") <> "" Then
                prefix = " (" & FieldListList("prefix") & ")"
            End If
            boundColumn.HeaderText = FieldListList("FieldName") + prefix
            boundColumn.HeaderStyle.Wrap = False
            boundColumn.DataFormatString = "{0:N" & FieldListList("NumberOfDps") & "}"
    End Select
End While

3 Answers, 1 is accepted

Sort by
0
Mira
Telerik team
answered on 28 Sep 2010, 01:26 PM
Hello Johnathan,

Straight to your questions:
  1. Please try setting the ListTextField and ListValueField properties of the column to the corresponding values.
  2. 3. You can use the following code to implement the desired functionality:
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            GridEditManager editMan = editedItem.EditManager;
      
            GridDropDownListColumnEditor editor = (GridDropDownListColumnEditor)(editMan.GetColumnEditor("ColumnUniqueName"));       
            RadComboBox combo = editor.ComboBoxControl;
            combo.AllowCustomText = true;
            combo.MarkFirstMatch = true;
            combo.DataSourse = //set the desired data source          
        }
    }

For additional information you can examine the Customize/Configure GridDropDownColumn help topic.

All the best,
Mira
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
Johnathan
Top achievements
Rank 1
answered on 28 Sep 2010, 04:01 PM
Hi Mira

Thanks for your reply.

I've got the combo populated but I still can't get the field to display correctly when in "read" mode and not editable / insert mode.
Also there seems to be some strange behaviour.

When in 'read' mode the Headertext appears correctly but the is no text data displayed in the table cells
When I click "Add New Record" or 'edit' the Headertext no longer appears, and there is still no data.
My previously working SQL insert no longer works as it relies on the unique column name  which no longer appears to be passed to the insert command event. The code you provided me to populate the combo is working fine on the  itemdatabound event so the column name is available here.. See pics below.



My Column creation code is:
Note fieldListList("Fieldname") contains the name (string) of the field in the database to display. Int he example above it is "Product" 
Dim boundColumn As GridDropDownColumn = New GridDropDownColumn()
                     boundColumn.DropDownControlType = GridDropDownColumnControlType.RadComboBox
                     boundColumn.UniqueName = FieldListList("FieldName")
                     boundColumn.DataField = FieldListList("FieldName")
                     boundColumn.HeaderText = FieldListList("FieldName")
                     boundColumn.ListTextField = FieldListList("FieldName")
                     boundColumn.ListValueField = FieldListList("FieldName")
                     RadGrid1.MasterTableView.Columns.Add(boundColumn)


thanks


Johnathan
0
Mira
Telerik team
answered on 29 Sep 2010, 01:58 PM
Hello Johnathan,

Please make sure that you create the grid according to the Programmatic creation help topic:
  • To define the structure of a RadGrid control that is declared in the ASPX page, use the Page_Load event handler. Columns and detail table should be added to the corresponding collection first, before the values for their properties are set. 
  • When generating RadGrid instance entirely in code, you should use the Page_Init event handler. Once the grid is created, add it to the Controls collection of a PlaceHolder control that is declared in the ASPX file. When generating a grid in the Page_Init event handler, grid columns should be added to the Controls collection of the MasterTableView after their attributes are set.

If the issue persists, please open a formal support ticket and send us a small working project, demonstrating your full setup and showing the unwanted behavior.
We will debug it locally and get back to you.

Kind regards,
Mira
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
Tags
Grid
Asked by
Johnathan
Top achievements
Rank 1
Answers by
Mira
Telerik team
Johnathan
Top achievements
Rank 1
Share this question
or