My problem is changing an auto-generated column editor type..
I want to change from a textbox control to a dropdownlist control using AutoGenerateColumns = true
Is there a different way to do what I'm doing below? I really hate deriving from GridTextBoxColumnEditor just to expose a DropDownList.
I want to change from a textbox control to a dropdownlist control using AutoGenerateColumns = true
Is there a different way to do what I'm doing below? I really hate deriving from GridTextBoxColumnEditor just to expose a DropDownList.
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="true" AllowAutomaticUpdates="true" OnNeedDataSource="RadGrid1_NeedDataSource" OnCreateColumnEditor="RadGrid1_CreateColumnEditor"> |
protected void RadGrid1_CreateColumnEditor(object sender, GridCreateColumnEditorEventArgs e) |
{ |
if (e.Column is GridBoundColumn) |
{ |
if ((e.Column as GridBoundColumn).DataField == "Code") |
{ |
DropDownList ddl = new DropDownList(); |
e.ColumnEditor = new CustomDropDownList(ddl); |
} |
} |
} |
// Derived class used to expose a dropdownlist
// (Notice i'm deriving from GridTextBoxColumnEditor)
// (Notice i'm deriving from GridTextBoxColumnEditor)
public class CustomDropDownList : GridTextBoxColumnEditor |
{ |
private DropDownList _ddl; |
public CustomDropDownList(DropDownList ddl) |
{ |
this._ddl = ddl; |
} |
protected override void LoadControlsFromContainer() |
{ |
this._ddl = this.ContainerControl.Controls[0] as DropDownList; |
} |
public override bool IsInitialized |
{ |
get |
{ |
return this._ddl != null; |
} |
} |
public override string Text |
{ |
get |
{ |
return this._ddl.SelectedValue; |
} |
set |
{ |
this._ddl.SelectedIndex = this._ddl.Items.IndexOf(this._ddl.Items.FindByValue(value)); |
} |
} |
protected override void AddControlsToContainer() |
{ |
this.ContainerControl.Controls.Add(this._ddl); |
} |
} |
10 Answers, 1 is accepted
0

Rocky
Top achievements
Rank 1
answered on 06 Sep 2009, 05:33 PM
I have no idea why the above post looks soo terrible, but I can't seem to reformat it to save my life. Sorry. Hope it's readable.
0

Rocky
Top achievements
Rank 1
answered on 06 Sep 2009, 06:33 PM
Maybe I should word it another way.
The grid will have a data source with a different select command potentially every time. There are some columns that I'll change for by their DataField property that will need to be converted to DropDownLIsts when edited. In order to change the editor control, the replacment control must derive from the control type you're trying to replace. Is there a different way to replace autogenerated editable controls to different types? (like drop down?)
The grid will have a data source with a different select command potentially every time. There are some columns that I'll change for by their DataField property that will need to be converted to DropDownLIsts when edited. In order to change the editor control, the replacment control must derive from the control type you're trying to replace. Is there a different way to replace autogenerated editable controls to different types? (like drop down?)
0
Hello Rocky,
It seems that you discovered a way to replace the default textbox column editor of auto-generated GridBoundColumn with dropdown list. Indeed there is a requirement your custom class to extend the GridTextBoxColumnEditor class (used as a base class for derived auto-generated textbox column editors) in order to assign an object of this class to e.ColumnEditor inside the OnCreateColumnEditor event handler.
I updated your Telerik points for sharing your implementation in this public forum thread.
Kind regards,
Sebastian
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
It seems that you discovered a way to replace the default textbox column editor of auto-generated GridBoundColumn with dropdown list. Indeed there is a requirement your custom class to extend the GridTextBoxColumnEditor class (used as a base class for derived auto-generated textbox column editors) in order to assign an object of this class to e.ColumnEditor inside the OnCreateColumnEditor event handler.
I updated your Telerik points for sharing your implementation in this public forum thread.
Kind regards,
Sebastian
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0

chris lively
Top achievements
Rank 1
answered on 28 Sep 2010, 06:39 AM
This code sample gets me SO CLOSE I can smell it.
However, it doesn't work when AllowMultiRowEdit is turned on...
I can get my drop downs enabled on only the last row in the grid. Any ideas how to get AutoGenerateColumns=true, AllowMultiRowEdit=true, and drop down lists to work together?
My grid has an unknown number of columns and rows, otherwise I'd just specify it all upfront.
However, it doesn't work when AllowMultiRowEdit is turned on...
I can get my drop downs enabled on only the last row in the grid. Any ideas how to get AutoGenerateColumns=true, AllowMultiRowEdit=true, and drop down lists to work together?
My grid has an unknown number of columns and rows, otherwise I'd just specify it all upfront.
0

chris lively
Top achievements
Rank 1
answered on 28 Sep 2010, 06:56 AM
Problem Solved. Rocky's original post had one little flaw. Namely that the DropDownList control was reused. This caused the last row to be the only row which actually got the controls.
So, I changed the definition to the following, their might be a better way (probably .clone()..) but for now this seems to work:
So, I changed the definition to the following, their might be a better way (probably .clone()..) but for now this seems to work:
public class DropDownListForTelerik : GridTextBoxColumnEditor {
private DropDownList _ddl;
private DropDownList _copyFrom;
public DropDownListForTelerik(DropDownList ddl) {
_copyFrom = ddl;
}
protected override void LoadControlsFromContainer() {
this._ddl = this.ContainerControl.Controls[0] as DropDownList;
}
public override bool IsInitialized {
get { return this._ddl != null; }
}
public override string Text {
get { return this._ddl.SelectedValue; }
set { this._ddl.SelectedIndex = this._ddl.Items.IndexOf(this._ddl.Items.FindByValue(value)); }
}
protected override void AddControlsToContainer() {
_ddl = new DropDownList();
foreach (ListItem item in _copyFrom.Items) {
ListItem i2 = new ListItem(item.Text, item.Value);
_ddl.Items.Add(i2);
}
this.ContainerControl.Controls.Add(_ddl);
}
}
0

Mark
Top achievements
Rank 1
answered on 21 Mar 2013, 03:17 PM
Can you please give some idea why I am getting an error using the method suggested in the thread? It works fine if I don't try to convert to a dropdownlistbox, which it's a GridBoundColumn with TextBox editor by default, but I like to show a combobox to show a list of values user can select when Edit button is pressed.
Help please
Help please
protected void InventorySecurity_CreateColumnEditor(object sender, GridCreateColumnEditorEventArgs e)
{
if (e.Column is GridBoundColumn)
{
if ((e.Column as GridBoundColumn).DataField == "SubjectOfferingFlag")
{
DropDownList ddl = new DropDownList();
//ddl.Items.Clear();
//ddl.Items.Insert(0, "0");
//ddl.Items.Insert(1, "1");
e.ColumnEditor = new CustomDropDownList(ddl);
}
}
}
public class CustomDropDownList : GridTextBoxColumnEditor
{
private DropDownList _ddl;
private DropDownList _copyFrom;
public CustomDropDownList(DropDownList ddl)
{
_copyFrom = ddl;
}
protected override void LoadControlsFromContainer()
{
this._ddl = this.ContainerControl.Controls[0] as DropDownList;
}
public override bool IsInitialized
{
get { return this._ddl != null; }
}
public override string Text
{
get { return this._ddl.SelectedValue; }
set { this._ddl.SelectedIndex = this._ddl.Items.IndexOf(this._ddl.Items.FindByValue(value)); }
}
protected override void AddControlsToContainer()
{
_ddl = new DropDownList();
foreach (ListItem item in _copyFrom.Items)
{
ListItem i2 = new ListItem(item.Text, item.Value);
_ddl.Items.Add(i2);
}
this.ContainerControl.Controls.Add(_ddl);
}
}
0
Hi,
The problem comes from the fact that you are trying to change the editor control type. The GridColumnEditor classes could be used to apply changes that are not greatly affecting the working process of the control. Changes like single-line to multi-line TextBox, DatePicker to DateTimePicker and so on. Basically the controls should be compatible with each, the relation should be either direct or indirect inheritance.
Greetings,
Andrey
the Telerik team
The problem comes from the fact that you are trying to change the editor control type. The GridColumnEditor classes could be used to apply changes that are not greatly affecting the working process of the control. Changes like single-line to multi-line TextBox, DatePicker to DateTimePicker and so on. Basically the controls should be compatible with each, the relation should be either direct or indirect inheritance.
Greetings,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0

Jeff
Top achievements
Rank 2
answered on 20 Mar 2015, 04:35 PM
Hi,
I'm trying to accomplish the same thing (convert specific columns to dropdownlist in the editors).
I have the same problem in that I don't know all of the columns but will know which ones (if they are there) need to be dropdownlist. I am using the code provided (converted to vb.net). When I try to edit/insert a row, I get an Object reference not set to an instance of an object. Below is the code from the CreateColumnEditor and the CustomDropDownList class.
Any help would be greatly appreciated.
Thanks,
Jeff
Protected Sub RadGrid1_CreateColumnEditor(sender As Object, e As GridCreateColumnEditorEventArgs) Handles RadGrid1.CreateColumnEditor
If TypeOf e.Column Is GridBoundColumn Then
If TryCast(e.Column, GridBoundColumn).DataField = "knowncolumn" Then
Dim ddl2 As New DropDownList()
ddl2.Items.Clear()
For Each row In knowncolumnDT.Rows
ddl2.Items.Add(row("field"))
Next
e.ColumnEditor = New CustomDropDownList(ddl2)
End If
End If
End Sub
Public Class CustomDropDownList
Inherits GridTextBoxColumnEditor
Private _ddl As DropDownList
Private _copyFrom As DropDownList
Public Sub New(ddl As DropDownList)
_copyFrom = ddl
End Sub
Protected Overrides Sub LoadControlsFromContainer()
Me._ddl = TryCast(Me.ContainerControl.Controls(0), DropDownList)
End Sub
Public Overrides ReadOnly Property IsInitialized() As Boolean
Get
Return Me._ddl IsNot Nothing
End Get
End Property
Public Overrides Property Text() As String
Get
Return Me._ddl.SelectedValue
End Get
Set(value As String)
Me._ddl.SelectedIndex = Me._ddl.Items.IndexOf(Me._ddl.Items.FindByValue(value))
End Set
End Property
Protected Overrides Sub AddControlsToContainer()
_ddl = New DropDownList()
For Each item As ListItem In _copyFrom.Items
Dim i2 As New ListItem(item.Text, item.Value)
_ddl.Items.Add(i2)
Next
Me.ContainerControl.Controls.Add(_ddl)
End Sub
End Class
I'm trying to accomplish the same thing (convert specific columns to dropdownlist in the editors).
I have the same problem in that I don't know all of the columns but will know which ones (if they are there) need to be dropdownlist. I am using the code provided (converted to vb.net). When I try to edit/insert a row, I get an Object reference not set to an instance of an object. Below is the code from the CreateColumnEditor and the CustomDropDownList class.
Any help would be greatly appreciated.
Thanks,
Jeff
Protected Sub RadGrid1_CreateColumnEditor(sender As Object, e As GridCreateColumnEditorEventArgs) Handles RadGrid1.CreateColumnEditor
If TypeOf e.Column Is GridBoundColumn Then
If TryCast(e.Column, GridBoundColumn).DataField = "knowncolumn" Then
Dim ddl2 As New DropDownList()
ddl2.Items.Clear()
For Each row In knowncolumnDT.Rows
ddl2.Items.Add(row("field"))
Next
e.ColumnEditor = New CustomDropDownList(ddl2)
End If
End If
End Sub
Public Class CustomDropDownList
Inherits GridTextBoxColumnEditor
Private _ddl As DropDownList
Private _copyFrom As DropDownList
Public Sub New(ddl As DropDownList)
_copyFrom = ddl
End Sub
Protected Overrides Sub LoadControlsFromContainer()
Me._ddl = TryCast(Me.ContainerControl.Controls(0), DropDownList)
End Sub
Public Overrides ReadOnly Property IsInitialized() As Boolean
Get
Return Me._ddl IsNot Nothing
End Get
End Property
Public Overrides Property Text() As String
Get
Return Me._ddl.SelectedValue
End Get
Set(value As String)
Me._ddl.SelectedIndex = Me._ddl.Items.IndexOf(Me._ddl.Items.FindByValue(value))
End Set
End Property
Protected Overrides Sub AddControlsToContainer()
_ddl = New DropDownList()
For Each item As ListItem In _copyFrom.Items
Dim i2 As New ListItem(item.Text, item.Value)
_ddl.Items.Add(i2)
Next
Me.ContainerControl.Controls.Add(_ddl)
End Sub
End Class
0

Jeff
Top achievements
Rank 2
answered on 20 Mar 2015, 07:25 PM
A little more information.
It seems related to the GridTextBoxColumnEditor.CreateControls function
[NullReferenceException: Object reference not set to an instance of an object.]
Telerik.Web.UI.GridTextBoxColumnEditor.CreateControls() +254
Telerik.Web.UI.GridColumnEditorBase.EnsureControlsCreated() +31
Telerik.Web.UI.GridColumnEditorBase.InitializeInControl(Control containerControl) +48
Telerik.Web.UI.GridBoundColumn.InitializeCell(TableCell cell, Int32 columnIndex, GridItem inItem) +360
Telerik.Web.UI.GridEditFormItem.InitializeAutogeneratedForm(GridColumn[] columns, ControlCollection controls, GridEditFormSettings formSettings) +2189
Telerik.Web.UI.GridEditFormItem.InitializeEditForm(GridColumn[] columns) +4264
Telerik.Web.UI.GridEditFormItem.SetupItem(Boolean dataBind, Object dataItem, GridColumn[] columns, ControlCollection rows) +248
Telerik.Web.UI.GridItemBuilder.CreateItems(GridGroupingContext group) +1036
Telerik.Web.UI.GridTableView.CreateItems(IEnumerator enumerator, GridColumn[] columns, ControlCollection controls) +160
Telerik.Web.UI.GridTableView.CreateControlHierarchy(Boolean useDataSource) +2143
Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource) +1365
System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +94
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +221
Telerik.Web.UI.GridTableView.PerformSelect() +243
Telerik.Web.UI.GridTableView.DataBind() +440
Telerik.Web.UI.GridTableView.Rebind() +209
Telerik.Web.UI.GridCommandEventArgs.ExecuteCommand(Object source) +193
Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e) +146
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +83
Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +101
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +83
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3803
It seems related to the GridTextBoxColumnEditor.CreateControls function
[NullReferenceException: Object reference not set to an instance of an object.]
Telerik.Web.UI.GridTextBoxColumnEditor.CreateControls() +254
Telerik.Web.UI.GridColumnEditorBase.EnsureControlsCreated() +31
Telerik.Web.UI.GridColumnEditorBase.InitializeInControl(Control containerControl) +48
Telerik.Web.UI.GridBoundColumn.InitializeCell(TableCell cell, Int32 columnIndex, GridItem inItem) +360
Telerik.Web.UI.GridEditFormItem.InitializeAutogeneratedForm(GridColumn[] columns, ControlCollection controls, GridEditFormSettings formSettings) +2189
Telerik.Web.UI.GridEditFormItem.InitializeEditForm(GridColumn[] columns) +4264
Telerik.Web.UI.GridEditFormItem.SetupItem(Boolean dataBind, Object dataItem, GridColumn[] columns, ControlCollection rows) +248
Telerik.Web.UI.GridItemBuilder.CreateItems(GridGroupingContext group) +1036
Telerik.Web.UI.GridTableView.CreateItems(IEnumerator enumerator, GridColumn[] columns, ControlCollection controls) +160
Telerik.Web.UI.GridTableView.CreateControlHierarchy(Boolean useDataSource) +2143
Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource) +1365
System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +94
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +221
Telerik.Web.UI.GridTableView.PerformSelect() +243
Telerik.Web.UI.GridTableView.DataBind() +440
Telerik.Web.UI.GridTableView.Rebind() +209
Telerik.Web.UI.GridCommandEventArgs.ExecuteCommand(Object source) +193
Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e) +146
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +83
Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +101
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +83
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3803
0

Jeff
Top achievements
Rank 2
answered on 20 Mar 2015, 07:31 PM
Found it!
the customdropdownlist class needs to inherit GridTextColumnEditor not GridTextBoxColumnEditor
Hope this helps someone else later.
Jeff
the customdropdownlist class needs to inherit GridTextColumnEditor not GridTextBoxColumnEditor
Hope this helps someone else later.
Jeff