Hi
I created this example to show problem that occurs in my real application.
This is my source code:
public class MyClass { public string MyProperty { get; set; } }public partial class Form1 : Form { private List<MyClass> _myList = new List<MyClass>(); public Form1() { InitializeComponent(); radGridView1.DataSource = _myList; radGridView1.Columns[0].Width = 200; } private void radGridView1_EditorRequired(object sender, Telerik.WinControls.UI.EditorRequiredEventArgs e) { RadDropDownListEditor editor = new RadDropDownListEditor(); RadDropDownListEditorElement editorElement = editor.EditorElement as RadDropDownListEditorElement; editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown; editorElement.AutoCompleteMode = AutoCompleteMode.SuggestAppend; e.Editor = editor; e.EditorType = typeof(RadDropDownListEditor); } }
When I want add new row I type something in cell and I press ENTER but new row is not added. Cell is cleared.
If I don`t use custom editor (comment out code in EditorRequired handler) on ENTER pressed new row is added with typed value.
How to solve this issue when I use custom editor?
Regards
29 Answers, 1 is accepted
Are you wanting auto complete on the add new row section? The sample below just shows a simple implementation of using a drop down column. The values will appear in a drop down in the add new row. Is this what you want to do? If you could let me know any changes you want to make, I'll do my best to help
thanks
Richard
Designer File
namespace RadGridView_Basic_C { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.radGridView1 = new Telerik.WinControls.UI.RadGridView(); ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit(); this.SuspendLayout(); // // radGridView1 // this.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill; this.radGridView1.Location = new System.Drawing.Point(0, 0); this.radGridView1.Name = "radGridView1"; this.radGridView1.Size = new System.Drawing.Size(457, 411); this.radGridView1.TabIndex = 0; this.radGridView1.Text = "radGridView1"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(457, 411); this.Controls.Add(this.radGridView1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit(); this.ResumeLayout(false); } #endregion private Telerik.WinControls.UI.RadGridView radGridView1; } } Form1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Telerik.WinControls.UI; using Telerik.WinControls; using Telerik.WinControls.Data; using Telerik.WinControls.UI.Export; using Telerik.WinControls.UI.Export.HTML; using Telerik.WinControls.RadControlSpy; namespace RadGridView_Basic_C { public partial class Form1 : Form { private List<Person> m_myList = new List<Person>(); private List<City> m_CityList = new List<City>(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.radGridView1.AutoGenerateColumns = false; this.radGridView1.AllowAddNewRow = true; this.radGridView1.AllowEditRow = true; m_myList.Add(new Person(1, "Richard", 1)); m_myList.Add(new Person(1, "Stew", 2)); m_myList.Add(new Person(1, "Chris", 3)); m_myList.Add(new Person(1, "Peter", 4)); radGridView1.DataSource = m_myList; m_CityList.Add(new City(1, "Bournemouth")); m_CityList.Add(new City(2, "Christchurch")); m_CityList.Add(new City(3, "Lymington")); m_CityList.Add(new City(4, "Bearwood")); m_CityList.Add(new City(5, "Southampton")); GridViewDecimalColumn idColumn = new GridViewDecimalColumn(); idColumn.Name = "Id"; idColumn.HeaderText = "Id"; idColumn.FieldName = "Id"; this.radGridView1.Columns.Add(idColumn); GridViewTextBoxColumn nameColumn = new GridViewTextBoxColumn(); nameColumn.Name = "Name"; nameColumn.HeaderText = "Name"; nameColumn.FieldName = "Name"; this.radGridView1.Columns.Add(nameColumn); GridViewComboBoxColumn cityColumn = new GridViewComboBoxColumn("City", "City"); cityColumn.DataSource = m_CityList; cityColumn.DisplayMember = "Name"; cityColumn.ValueMember = "Id"; this.radGridView1.Columns.Add(cityColumn); } } class Person { public Person() {} public Person(int id, string name, int city) { this.Id = id; this.Name = name; this.City = city; } public int Id { get; set; } public string Name { get; set; } public int City { get; set; } } class City { public City() {} public City(int Id, string name) { this.Id = Id; this.Name = name; } public int Id { get; set; } public string Name { get; set; } } } Designer File
namespace RadGridView_Basic_C { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.radGridView1 = new Telerik.WinControls.UI.RadGridView(); ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit(); this.SuspendLayout(); // // radGridView1 // this.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill; this.radGridView1.Location = new System.Drawing.Point(0, 0); this.radGridView1.Name = "radGridView1"; this.radGridView1.Size = new System.Drawing.Size(457, 411); this.radGridView1.TabIndex = 0; this.radGridView1.Text = "radGridView1"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(457, 411); this.Controls.Add(this.radGridView1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit(); this.ResumeLayout(false); } #endregion private Telerik.WinControls.UI.RadGridView radGridView1; } } Form1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Telerik.WinControls.UI; using Telerik.WinControls; using Telerik.WinControls.Data; using Telerik.WinControls.UI.Export; using Telerik.WinControls.UI.Export.HTML; using Telerik.WinControls.RadControlSpy; namespace RadGridView_Basic_C { public partial class Form1 : Form { private List<Person> m_myList = new List<Person>(); private List<City> m_CityList = new List<City>(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.radGridView1.AutoGenerateColumns = false; this.radGridView1.AllowAddNewRow = true; this.radGridView1.AllowEditRow = true; m_myList.Add(new Person(1, "Richard", "Bournemouth")); m_myList.Add(new Person(1, "Stew", "Christchurch")); m_myList.Add(new Person(1, "Chris", "Bearwood")); m_myList.Add(new Person(1, "Peter", "Southampton")); radGridView1.DataSource = m_myList; m_CityList.Add(new City(1, "Bournemouth")); m_CityList.Add(new City(2, "Christchurch")); m_CityList.Add(new City(3, "Lymington")); m_CityList.Add(new City(4, "Bearwood")); m_CityList.Add(new City(5, "Southampton")); GridViewDecimalColumn idColumn = new GridViewDecimalColumn(); idColumn.Name = "Id"; idColumn.HeaderText = "Id"; idColumn.FieldName = "Id"; this.radGridView1.Columns.Add(idColumn); GridViewTextBoxColumn nameColumn = new GridViewTextBoxColumn(); nameColumn.Name = "Name"; nameColumn.HeaderText = "Name"; nameColumn.FieldName = "Name"; this.radGridView1.Columns.Add(nameColumn); GridViewTextBoxColumn cityColumn = new GridViewTextBoxColumn(); cityColumn.Name = "City"; cityColumn.HeaderText = "City"; cityColumn.FieldName = "City"; this.radGridView1.Columns.Add(cityColumn); this.radGridView1.EditorRequired += new Telerik.WinControls.UI.EditorRequiredEventHandler(this.radGridView1_EditorRequired); } private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e) { if (this.radGridView1.CurrentColumn.Name == "City") { RadDropDownListEditor editor = new RadDropDownListEditor(); RadDropDownListEditorElement editorElement = editor.EditorElement as RadDropDownListEditorElement; editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown; editorElement.AutoCompleteMode = AutoCompleteMode.SuggestAppend; editorElement.DataSource = m_CityList; editorElement.DisplayMember = "Name"; editorElement.ValueMember = "Name"; e.Editor = editor; e.EditorType = typeof(RadDropDownListEditor); } } } class Person { public Person() {} public Person(int id, string name, string city) { this.Id = id; this.Name = name; this.City = city; } public int Id { get; set; } public string Name { get; set; } public string City { get; set; } } class City { public City() {} public City(int Id, string name) { this.Id = Id; this.Name = name; } public int Id { get; set; } public string Name { get; set; } } } Hope that helps
Richard
Hi
It doesn`t solve my problem.
- I cannot use column
GridViewComboBoxColumnbecause I need drop down control only in some rows. - In your code (with EditorRequired handler) source for RadDropDownListEditorElement is list with cities. Object city has id and name – if I type something that is not in this list and I press enter cell is cleared.
In my case source is just list with strings and I want have possibility typing also something new that is not in the list. Good example is filed in web browser where you type www address – you can type something new or selected address from drop down list.
Regards
Please can you try the following
Dersigner File
namespace RadGridView_Basic_C { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.radGridView1 = new Telerik.WinControls.UI.RadGridView(); ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit(); this.SuspendLayout(); // // radGridView1 // this.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill; this.radGridView1.Location = new System.Drawing.Point(0, 0); this.radGridView1.Name = "radGridView1"; this.radGridView1.Size = new System.Drawing.Size(457, 411); this.radGridView1.TabIndex = 0; this.radGridView1.Text = "radGridView1"; this.radGridView1.EditorRequired += new Telerik.WinControls.UI.EditorRequiredEventHandler(this.radGridView1_EditorRequired); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(457, 411); this.Controls.Add(this.radGridView1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit(); this.ResumeLayout(false); } #endregion private Telerik.WinControls.UI.RadGridView radGridView1; } } Form1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Telerik.WinControls.UI; using Telerik.WinControls; using Telerik.WinControls.Data; using Telerik.WinControls.UI.Export; using Telerik.WinControls.UI.Export.HTML; using Telerik.WinControls.RadControlSpy; namespace RadGridView_Basic_C { public partial class Form1 : Form { private List<Person> m_myList = new List<Person>(); List<String> cities = new List<string>(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.radGridView1.AutoGenerateColumns = false; this.radGridView1.AllowAddNewRow = true; this.radGridView1.AllowEditRow = true; m_myList.Add(new Person(1, "Richard", "Bournemouth")); m_myList.Add(new Person(1, "Stew", "Christchurch")); m_myList.Add(new Person(1, "Chris", "Bearwood")); m_myList.Add(new Person(1, "Peter", "Southampton")); radGridView1.DataSource = m_myList; GridViewDecimalColumn idColumn = new GridViewDecimalColumn(); idColumn.Name = "Id"; idColumn.HeaderText = "Id"; idColumn.FieldName = "Id"; this.radGridView1.Columns.Add(idColumn); GridViewTextBoxColumn nameColumn = new GridViewTextBoxColumn(); nameColumn.Name = "Name"; nameColumn.HeaderText = "Name"; nameColumn.FieldName = "Name"; this.radGridView1.Columns.Add(nameColumn); GridViewTextBoxColumn cityColumn = new GridViewTextBoxColumn(); cityColumn.Name = "City"; cityColumn.HeaderText = "City"; cityColumn.FieldName = "City"; this.radGridView1.Columns.Add(cityColumn); this.radGridView1.CellEditorInitialized += new Telerik.WinControls.UI.GridViewCellEventHandler(this.radGridView1_CellEditorInitialized); } private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e) { if (e.ActiveEditor is CustomEditor) { var dropDowneditor = e.ActiveEditor as CustomEditor; if (dropDowneditor == null) { return; } var hostItem = dropDowneditor.EditorElement as RadHostItem; var dropDown = hostItem.HostedControl as RadDropDownList; var autocompleteStringCollection = new AutoCompleteStringCollection(); foreach (var row in radGridView1.Rows) { var value = row.Cells["City"].Value; if (value != null) { var valueString = value.ToString(); if (!autocompleteStringCollection.Contains(valueString)) { autocompleteStringCollection.Add(valueString); } } } dropDown.DataSource = autocompleteStringCollection; dropDown.AutoCompleteMode = AutoCompleteMode.SuggestAppend; dropDown.AutoCompleteDataSource = autocompleteStringCollection; dropDown.Focus(); dropDown.DropDownListElement.TextBox.KeyDown += new KeyEventHandler(TextBox_KeyDown); } } private void TextBox_KeyDown(Object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { this.radGridView1.EndEdit(); } } private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e) { if (this.radGridView1.CurrentColumn.Name == "City") { if (e.EditorType == typeof(RadTextBoxEditor)) { e.EditorType = typeof(CustomEditor); var editor = new CustomEditor(); } } } } class Person { public Person() { } public Person(int id, string name, string city) { this.Id = id; this.Name = name; this.City = city; } public int Id { get; set; } public string Name { get; set; } public string City { get; set; } } public class CustomEditor : BaseGridEditor { private RadDropDownList radDropDown; public CustomEditor() { radDropDown = new RadDropDownList(); } protected override RadElement CreateEditorElement() { return new RadHostItem(radDropDown); } public override object Value { get { return radDropDown.Text; } set { radDropDown.Text = value.ToString(); } } } } Hope that helps
Richard
It is almost perfect but there is one issue.
If I select some cell in column city and I expand list 2, 3 times I am not able to expand list more times.
When I click button with arrow down list is expanded but immediately collapsed – don’t change selected cell during these clicks to see this problem.
Any ideas?
Regards
Yes, I see now that this is an issue. I'll look into it. Perhaps there is something I've done wrong. I'll try and get back to you as soon as I can
Richard
ok, this should be fine now. Replace the CustomEditor that I gave you before with this one.
public class CustomEditor : BaseGridEditor { private RadDropDownList radDropDown; public CustomEditor() { radDropDown = new RadDropDownList(); radDropDown.DropDownListElement.SelectionMode = SelectionMode.One; radDropDown.PopupClosed += new RadPopupClosedEventHandler(radDropDown_PopUpClosed); } private void radDropDown_PopUpClosed(Object sender, RadPopupClosedEventArgs e) { this.radDropDown.DropDownListElement.TextBox.TextBoxItem.PerformClick(); } protected override RadElement CreateEditorElement() { return new RadHostItem(radDropDown); } public override object Value { get { return radDropDown.Text; } set { radDropDown.Text = value.ToString(); } } } Hope that helps
Richard
It did not help.
I still have the same problem.
If I click couple times on button with arrow down (on the same cell) list is expanded and collapsed immediately.
Richard
Ok, my fault, we didn't need the custom editor. Please can you try the following. I'm pretty confident that this is fine now, but any issues let me know
Designer File
namespace RadGridView_Basic_C { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.radGridView1 = new Telerik.WinControls.UI.RadGridView(); ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit(); this.SuspendLayout(); // // radGridView1 // this.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill; this.radGridView1.Location = new System.Drawing.Point(0, 0); this.radGridView1.Name = "radGridView1"; this.radGridView1.Size = new System.Drawing.Size(457, 411); this.radGridView1.TabIndex = 0; this.radGridView1.Text = "radGridView1"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(457, 411); this.Controls.Add(this.radGridView1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit(); this.ResumeLayout(false); } #endregion private Telerik.WinControls.UI.RadGridView radGridView1; } } Form1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Telerik.WinControls.UI; using Telerik.WinControls; using Telerik.WinControls.Data; using Telerik.WinControls.UI.Export; using Telerik.WinControls.UI.Export.HTML; using Telerik.WinControls.RadControlSpy; namespace RadGridView_Basic_C { public partial class Form1 : Form { private List<Person> m_myList = new List<Person>(); List<String> cities = new List<string>(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.radGridView1.AutoGenerateColumns = false; this.radGridView1.AllowAddNewRow = true; this.radGridView1.AllowEditRow = true; m_myList.Add(new Person(1, "Richard", "Bournemouth")); m_myList.Add(new Person(1, "Stew", "Christchurch")); m_myList.Add(new Person(1, "Chris", "Bearwood")); m_myList.Add(new Person(1, "Peter", "Southampton")); radGridView1.DataSource = m_myList; GridViewDecimalColumn idColumn = new GridViewDecimalColumn(); idColumn.Name = "Id"; idColumn.HeaderText = "Id"; idColumn.FieldName = "Id"; this.radGridView1.Columns.Add(idColumn); GridViewTextBoxColumn nameColumn = new GridViewTextBoxColumn(); nameColumn.Name = "Name"; nameColumn.HeaderText = "Name"; nameColumn.FieldName = "Name"; this.radGridView1.Columns.Add(nameColumn); GridViewComboBoxColumn cityColumn = new GridViewComboBoxColumn(); cityColumn.Name = "City"; cityColumn.HeaderText = "City"; cityColumn.FieldName = "City"; this.radGridView1.Columns.Add(cityColumn); this.radGridView1.CellEditorInitialized += new Telerik.WinControls.UI.GridViewCellEventHandler(this.radGridView1_CellEditorInitialized); } private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e) { RadDropDownListEditor listEditor = this.radGridView1.ActiveEditor as RadDropDownListEditor; if (listEditor == null) { return; } RadDropDownListEditorElement editorElement = listEditor.EditorElement as RadDropDownListEditorElement; editorElement.AutoCompleteMode = AutoCompleteMode.SuggestAppend; editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown; var autocompleteStringCollection = new AutoCompleteStringCollection(); foreach (var row in radGridView1.Rows) { var value = row.Cells["City"].Value; if (value != null) { var valueString = value.ToString(); if (!autocompleteStringCollection.Contains(valueString)) { autocompleteStringCollection.Add(valueString); } } } editorElement.DataSource = autocompleteStringCollection; editorElement.SelectedValue = this.radGridView1.CurrentCell.Value; } private void TextBox_KeyDown(Object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { this.radGridView1.EndEdit(); } } } class Person { public Person() { } public Person(int id, string name, string city) { this.Id = id; this.Name = name; this.City = city; } public int Id { get; set; } public string Name { get; set; } public string City { get; set; } } } Thanks
Richard
Ok problem with immediately collapsing list is resolved but we returned to my first issue.
When I type something that is not in the list and I press enter value in cell is changed to value from list, in the result typed value that is not in the list is canceled.
There is also another issue: if you click couple times in different cells from column city after some click text in clicked cell will be changed to empty.
Please can you try this. I'm rather busy at the moment so the amount of time I can spend on it is limited, but if you let me know how that is, i'll spend some more on it this evening if needs be
Designer
namespace RadGridView_Basic_C { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.radGridView1 = new Telerik.WinControls.UI.RadGridView(); ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit(); this.SuspendLayout(); // // radGridView1 // this.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill; this.radGridView1.Location = new System.Drawing.Point(0, 0); this.radGridView1.Name = "radGridView1"; this.radGridView1.Size = new System.Drawing.Size(457, 411); this.radGridView1.TabIndex = 0; this.radGridView1.Text = "radGridView1"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(457, 411); this.Controls.Add(this.radGridView1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit(); this.ResumeLayout(false); } #endregion private Telerik.WinControls.UI.RadGridView radGridView1; } } Form1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Telerik.WinControls.UI; using Telerik.WinControls; using Telerik.WinControls.Data; using Telerik.WinControls.UI.Export; using Telerik.WinControls.UI.Export.HTML; using Telerik.WinControls.RadControlSpy; namespace RadGridView_Basic_C { public partial class Form1 : Form { private List<Person> m_myList = new List<Person>(); List<String> cities = new List<string>(); private String editorText = ""; private RadTextBoxItem textBoxItem; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.radGridView1.AutoGenerateColumns = false; this.radGridView1.AllowAddNewRow = true; this.radGridView1.AllowEditRow = true; m_myList.Add(new Person(1, "Richard", "Bournemouth")); m_myList.Add(new Person(1, "Stew", "Christchurch")); m_myList.Add(new Person(1, "Chris", "Bearwood")); m_myList.Add(new Person(1, "Peter", "Southampton")); radGridView1.DataSource = m_myList; GridViewDecimalColumn idColumn = new GridViewDecimalColumn(); idColumn.Name = "Id"; idColumn.HeaderText = "Id"; idColumn.FieldName = "Id"; this.radGridView1.Columns.Add(idColumn); GridViewTextBoxColumn nameColumn = new GridViewTextBoxColumn(); nameColumn.Name = "Name"; nameColumn.HeaderText = "Name"; nameColumn.FieldName = "Name"; this.radGridView1.Columns.Add(nameColumn); GridViewComboBoxColumn cityColumn = new GridViewComboBoxColumn(); cityColumn.Name = "City"; cityColumn.HeaderText = "City"; cityColumn.FieldName = "City"; this.radGridView1.Columns.Add(cityColumn); this.radGridView1.CellEditorInitialized += new Telerik.WinControls.UI.GridViewCellEventHandler(this.radGridView1_CellEditorInitialized); } private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e) { RadDropDownListEditor listEditor = this.radGridView1.ActiveEditor as RadDropDownListEditor; if (listEditor == null) { return; } RadDropDownListEditorElement editorElement = listEditor.EditorElement as RadDropDownListEditorElement; editorElement.AutoCompleteMode = AutoCompleteMode.SuggestAppend; editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown; var autocompleteStringCollection = new AutoCompleteStringCollection(); foreach (var row in radGridView1.Rows) { var value = row.Cells["City"].Value; if (value != null) { var valueString = value.ToString(); if (!autocompleteStringCollection.Contains(valueString)) { autocompleteStringCollection.Add(valueString); } } } textBoxItem = editorElement.TextBox.TextBoxItem; editorElement.DataSource = autocompleteStringCollection; editorElement.SelectedValue = this.radGridView1.CurrentCell.Value; editorElement.TextBox.TextBoxItem.TextChanging += new TextChangingEventHandler(TextBoxItem_TextChanging); editorElement.TextBox.TextBoxItem.KeyDown += new KeyEventHandler(TextBoxItem_KeyDown); } private void TextBoxItem_TextChanging(Object sender, TextChangingEventArgs e) { if (textBoxItem != null) { if (textBoxItem.Text.Length > 0) { editorText = textBoxItem.Text; } } } private void TextBoxItem_KeyDown(Object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { this.radGridView1.CurrentCell.Value = editorText; this.radGridView1.EndEdit(); } } } class Person { public Person() { } public Person(int id, string name, string city) { this.Id = id; this.Name = name; this.City = city; } public int Id { get; set; } public string Name { get; set; } public string City { get; set; } } } Regards,
Richard
This is better but not enough good to use it in my real application.
There are two problems:
- If you type something new in city cell (for existing row) and press enter text in cell is ok but if you again click on the same cell to change text after switching to edit mode text in cell is cleared.
- If I want add new row I fill Id, Name and City (I type something that is not on the list) but after adding row city in added row is empty.
Thanks for help – I was trying to figure out something but I failed.
It is very annoying that it is so difficult implement so simple feature using telerik control.
This now seems to work for me. I'll also post a video in case I've missed anything. To be honest, there is likely to be an easier way that I have missed, but if it helps you, I'm happy to give it a go.
Richard
Designer File
namespace RadGridView_Basic_C { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.radGridView1 = new Telerik.WinControls.UI.RadGridView(); ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit(); this.SuspendLayout(); // // radGridView1 // this.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill; this.radGridView1.Location = new System.Drawing.Point(0, 0); this.radGridView1.Name = "radGridView1"; this.radGridView1.Size = new System.Drawing.Size(457, 411); this.radGridView1.TabIndex = 0; this.radGridView1.Text = "radGridView1"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(457, 411); this.Controls.Add(this.radGridView1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit(); this.ResumeLayout(false); } #endregion private Telerik.WinControls.UI.RadGridView radGridView1; } } Form 1
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Telerik.WinControls.UI; using Telerik.WinControls; using Telerik.WinControls.Data; using Telerik.WinControls.UI.Export; using Telerik.WinControls.UI.Export.HTML; using Telerik.WinControls.RadControlSpy; namespace RadGridView_Basic_C { public partial class Form1 : Form { private List<Person> m_myList = new List<Person>(); List<String> cities = new List<string>(); private String editorText = ""; private RadTextBoxItem textBoxItem; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.radGridView1.AutoGenerateColumns = false; this.radGridView1.AllowAddNewRow = true; this.radGridView1.AllowEditRow = true; m_myList.Add(new Person(1, "Richard", "Bournemouth")); m_myList.Add(new Person(1, "Stew", "Christchurch")); m_myList.Add(new Person(1, "Chris", "Bearwood")); m_myList.Add(new Person(1, "Peter", "Southampton")); radGridView1.DataSource = m_myList; GridViewDecimalColumn idColumn = new GridViewDecimalColumn(); idColumn.Name = "Id"; idColumn.HeaderText = "Id"; idColumn.FieldName = "Id"; this.radGridView1.Columns.Add(idColumn); GridViewTextBoxColumn nameColumn = new GridViewTextBoxColumn(); nameColumn.Name = "Name"; nameColumn.HeaderText = "Name"; nameColumn.FieldName = "Name"; this.radGridView1.Columns.Add(nameColumn); GridViewComboBoxColumn cityColumn = new GridViewComboBoxColumn(); cityColumn.Name = "City"; cityColumn.HeaderText = "City"; cityColumn.FieldName = "City"; this.radGridView1.Columns.Add(cityColumn); this.radGridView1.CellEndEdit += new Telerik.WinControls.UI.GridViewCellEventHandler(this.radGridView1_CellEndEdit); this.radGridView1.CellEditorInitialized += new Telerik.WinControls.UI.GridViewCellEventHandler(this.radGridView1_CellEditorInitialized); } private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e) { RadDropDownListEditor listEditor = this.radGridView1.ActiveEditor as RadDropDownListEditor; if (listEditor == null) { return; } RadDropDownListEditorElement editorElement = listEditor.EditorElement as RadDropDownListEditorElement; editorElement.AutoCompleteMode = AutoCompleteMode.SuggestAppend; editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown; var autocompleteStringCollection = new AutoCompleteStringCollection(); foreach (var row in radGridView1.Rows) { var value = row.Cells["City"].Value; if (value != null) { var valueString = value.ToString(); if (!autocompleteStringCollection.Contains(valueString)) { autocompleteStringCollection.Add(valueString); } } } textBoxItem = editorElement.TextBox.TextBoxItem; editorElement.DataSource = autocompleteStringCollection; editorElement.SelectedValue = this.radGridView1.CurrentCell.Value; editorElement.TextBox.TextBoxItem.TextChanging += new TextChangingEventHandler(TextBoxItem_TextChanging); } private void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e) { if (editorText.Length > 0) { this.radGridView1.CurrentCell.Value = editorText; editorText = ""; } } private void TextBoxItem_TextChanging(Object sender, TextChangingEventArgs e) { if (textBoxItem != null) { if (textBoxItem.Text.Length > 0) { editorText = textBoxItem.Text; } } } class Person { public Person() { } public Person(int id, string name, string city) { this.Id = id; this.Name = name; this.City = city; } public int Id { get; set; } public string Name { get; set; } public string City { get; set; } } } } Video to follow
Richard
I added this line in handler of CellEditorInitialized:
if (this.radGridView1.CurrentCell.Value != null) { editorElement.TextBox.TextBoxItem.Text = this.radGridView1.CurrentCell.Value.ToString(); } else{ editorElement.TextBox.TextBoxItem.Text = string.Empty; } and in your code there was missing handler for EditorRequired, probably you just forgot copy it.
There is only I think the last small issue.
Sometimes when I type something that starts from characters that match to items in the list I don`t see hint (list) with matching items. This is random I think. Do you see this problem also in you app?
I marked this as answer because it is almost perfect and you helped me a lot.
Regards
Yes, I just missed out an event handler when I pasted this in I think. I'm really glad that this has helped you. To be honest, I've tried this now quite a few times and I haven't seen the autocomplete behavior not working. Perhaps it would be good to try it with an ordinary RadDropDown list to see if it does it there too.
If there's anything else, just let me know.
Thanks. All the best
Richard
I found new problem in DropDown style.
I cannot use arrows buttons left and right to move cursorL
Do you have any ideas?
Regards
Hope you're well. I'm not quite sure what you mean. I have recorded a quick video based on the code that I gave you that you marked as answer above. As you can see, as I move between cells and up and down, I used the keyboard all the time.
The video can be found here
Regards,
Richard
Thanks I am fine, I hope you also.
Your video does not show my problem.
Let say that I have text: abcdefg
I want set text cursor (small vertical line) between ‘c’ and ‘d’. I want do it using keyboard buttons: arrow left or arrow right.
Regards
I see. I'll see if I can find a way to get that to work in the desired way.
regards,
Richard
It would be great if you found some fix.
I missed this problem and it is already in my application – QA found this bug during testing.
I've just tried this on a normal situation GridViewComboBoxColumn set to
Telerik.WinControls.RadDropDownStyle.DropDown;Richard
I have checked sample from telerik demo: DropDown&List -> DropDownList -> AutoComplete (example with countries).
In this example you can use keys arrow left and arrow right to move cursor without any problems.
I think current behavior for GridViewComboBoxColumn in DropDown/AutoComplete mode is not desired – in my opinion it is bug.
Regards
Yes, I see that it works as you'd expect if it's just a RadDropDownList. I'm not sure at the moment if how it is behaving in a GridViewComboBoxColumn is expected, but I tend to agree. It's not right that the left and right arrows don't work at all (they work as you'd expect in a GridViewTextBoxColumn. I'll keep looking. I'm sure this can't be too difficult but I haven't got there with it yet.
Richard
The RadDropDownListEditor behavior described by you is the intended one. This editor is usually used to select an option from a list and this navigation is convenient for this scenario.
You can accomplish your task by creating a custom editor as in the following code snippet:
private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e){ if (e.EditorType == typeof(RadDropDownListEditor)) { e.EditorType = typeof(CustomEditor); }}public class CustomEditor : RadDropDownListEditor{ public override void OnKeyDown(KeyEventArgs e) { if (!(e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)) { base.OnKeyDown(e); } } public override void OnKeyUp(KeyEventArgs e) { if (!(e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)) { base.OnKeyDown(e); } }}I hope it helps.
Best regards,
Alexander
the Telerik team
thanks a lot