Please, can you help me with this question:
I have a grid with combobox column. All of these comboboxes have different DataSorce property (assigned thouth intercepting CellEditorInitialized event):
| Private Sub gridSort_CellEditorInitialized(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles gridSort.CellEditorInitialized |
| Dim i As Integer |
| If gridSort.CurrentColumn.HeaderText = "Order" Then |
| Dim editor As Telerik.WinControls.UI.RadComboBoxEditor = gridSort.ActiveEditor |
| Dim editorElement As Telerik.WinControls.UI.RadComboBoxEditorElement = editor.EditorElement |
| If gridSort.Rows.IndexOf(gridSort.CurrentRow) = 1 Then |
| i = editorElement.SelectedIndex |
| editorElement.DataSource = New String() {"Minimized", "Maximized"} |
| editorElement.SelectedIndex = i |
| If i < 0 Then |
| Dim ii As Integer = 0 |
| For Each ss As String In editorElement.DataSource |
| If ss = gridSort.CurrentRow.Cells.Item(1).Value Then |
| editorElement.SelectedIndex = ii |
| Exit For |
| End If |
| ii += 1 |
| Next |
| End If |
| Else |
| i = editorElement.SelectedIndex |
| editorElement.DataSource = New String() {"Ascending", "Descending"} |
| editorElement.SelectedIndex = i |
| End If |
| End If |
| End Sub |
1. Every time editorElement.SelectedIndex have value -1 (and I must set value by searching throuh DataSource list and comparing with cell Value). For values Minimized/Maximized it's done and work fine, but for values Ascending/Descending it's not and when I click on combobox field - it's filled by empty sring.
Comparing with Value of cell is not fine because here can be some duplicated values (sic!) and I can not determine what value was in this cell before. Is here some method of determine SelectedIndex after changing of value?
2. I want to collect data choosen by user. But ComboBox cell in grid have not SelectedIndex (or I can not find it?) and I must to obtain it from Value property. Problem is similiar with previous question - how can I find a real position of value in DataSource list?
8 Answers, 1 is accepted
Here is sample code to demonstrate the required functionality:
| Imports Telerik.WinControls.UI |
| Public Class Form1 |
| Dim list As List(Of String) = New List(Of String) |
| Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load |
| list.Add("Value1") |
| list.Add("Value2") |
| list.Add("Value3") |
| list.Add("Value4") |
| Dim column As GridViewComboBoxColumn = New GridViewComboBoxColumn |
| column.DataSource = list |
| Me.RadGridView1.Columns.Add(column) |
| End Sub |
| Private Sub RadGridView1_CellBeginEdit(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.GridViewCellCancelEventArgs) Handles RadGridView1.CellBeginEdit |
| Dim editor As IInputEditor = Me.RadGridView1.ActiveEditor |
| If TypeOf (editor) Is RadComboBoxEditor Then |
| Dim comboElement As RadComboBoxElement = CType(CType(editor, RadComboBoxEditor).EditorElement, RadComboBoxElement) |
| AddHandler (comboElement.SelectedIndexChanged), AddressOf SelectedIndexChanged |
| End If |
| End Sub |
| Private Sub RadGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles RadGridView1.CellEndEdit |
| Dim editor As IInputEditor = Me.RadGridView1.ActiveEditor |
| If TypeOf (editor) Is RadComboBoxEditor Then |
| Dim comboElement As RadComboBoxElement = CType(CType(editor, RadComboBoxEditor).EditorElement, RadComboBoxElement) |
| RemoveHandler (comboElement.SelectedIndexChanged), AddressOf SelectedIndexChanged |
| End If |
| End Sub |
| Private Sub SelectedIndexChanged(ByVal sender As System.Object, ByVal e As EventArgs) |
| Dim index As Integer = CType(sender, RadComboBoxElement).SelectedIndex |
| If index > -1 Then |
| MessageBox.Show("Selected Index changed. Value: " + list(index)) |
| End If |
| End Sub |
| End Class |
I hope I have addressed the issue correctly. Please write again if you need further assistance.
All the best,
Victor
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
| private void radGridView2_CellBeginEdit(object sender, GridViewCellCancelEventArgs e) |
| { |
| IInputEditor editor = this.radGridView2.ActiveEditor; |
| if (editor.GetType() == typeof(RadComboBoxEditor)) |
| { |
| RadComboBoxElement comboElement = (RadComboBoxElement)((RadComboBoxEditor)editor).EditorElement; |
| comboElement.SelectedIndexChanged += new EventHandler(comboElement_SelectedIndexChanged); |
| } |
| } |
| private void radGridView2_CellEndEdit(object sender, GridViewCellEventArgs e) |
| { |
| IInputEditor editor = this.radGridView2.ActiveEditor; |
| if (editor.GetType() == typeof(RadComboBoxEditor)) |
| { |
| RadComboBoxElement comboElement = (RadComboBoxElement)((RadComboBoxEditor)editor).EditorElement; |
| comboElement.SelectedIndexChanged -= new EventHandler(comboElement_SelectedIndexChanged); |
| } |
| } |
| void comboElement_SelectedIndexChanged(object sender, EventArgs e) |
| { |
| int index = (int)(((RadComboBoxElement)sender).SelectedIndex); |
| MessageBox.Show("Selected Index Changed. Value: " + index); |
| } |
Well, I didn't bother using the index to lookup a value in a list, but that's even more trivial to finish out.
Anyway, doing this has given me a hook into the indexchanged event which will allow me to apply the logic I needed. I'm so pleased, thank you for the contribution.
Kind regards,
Victor
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.
I am trying to implement a logic similar to above. The code is running fine for the first time, but when you select other row, the selected index property first shows previous selected index and then current index. Please help me.
Thanks in advance.
Regards,
Saurabh Garg
Thank you for writing. Please explain in more details what the issue is since I cannot reproduce the behavior you described. Some sample code would be most illustrative. I am looking forward to your reply.
Regards,
Victor
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.
Victor here is my code. Please try to edit more than one row to show it's behaviour, i hope that will generate the required behaviour.
| 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; |
| namespace TelerikGridEditing |
| { |
| public partial class Form1 : Form |
| { |
| public Form1() |
| { |
| InitializeComponent(); |
| } |
| private void Form1_Load(object sender, EventArgs e) |
| { |
| bindGrid(); |
| GridLayout(); |
| } |
| private void bindGrid() |
| { |
| DataTable dt = new DataTable(); |
| dt.Columns.Add("ProductId"); |
| dt.Columns.Add("ProductName"); |
| dt.Columns.Add("ProductDesciption"); |
| dt.Rows.Add(1, "IPod Classic 80GB", "Description of product"); |
| dt.Rows.Add(2, "IPod Touch 32GB", "Description of product"); |
| dt.Rows.Add(3, "DishWasher", "Description of product"); |
| dt.Rows.Add(4, "Microwave Oven", "Description of product"); |
| dt.Rows.Add(5, "WashingMachine", "Description of product"); |
| dt.Rows.Add(6, "Coffee Mug", "Description of product"); |
| dt.Rows.Add(7, "Umbrella", "Description of product"); |
| GridViewTextBoxColumn col1 = new GridViewTextBoxColumn(); |
| col1.FieldName = "ProductId"; |
| GridViewTextBoxColumn col2 = new GridViewTextBoxColumn(); |
| col1.FieldName = "ProductName"; |
| GridViewTextBoxColumn col3 = new GridViewTextBoxColumn(); |
| col1.FieldName = "ProductDesciption"; |
| GridViewComboBoxColumn col4 = new GridViewComboBoxColumn(); |
| DataTable dtQty = new DataTable(); |
| dtQty.Columns.Add("dataValue"); |
| dtQty.Columns.Add("displayValue"); |
| for (int i = 0; i <= 10; i++) |
| dtQty.Rows.Add(i, i); |
| col4.DataSource = dtQty; |
| col4.ValueMember = "dataValue"; |
| col4.DisplayMember = "displayValue"; |
| col4.FieldName = "dataValue"; |
| radGridView1.MasterGridViewTemplate.Columns.Add(col1); |
| radGridView1.MasterGridViewTemplate.Columns.Add(col2); |
| radGridView1.MasterGridViewTemplate.Columns.Add(col3); |
| radGridView1.MasterGridViewTemplate.Columns.Add(col4); |
| foreach (DataRow dr in dt.Rows) |
| radGridView1.MasterGridViewTemplate.Rows.Add(dr["ProductId"], dr["ProductName"], dr["ProductDesciption"], "0"); |
| } |
| private void GridLayout() |
| { |
| radGridView1.Columns[0].Width = 50; |
| radGridView1.Columns[1].Width = 100; |
| radGridView1.Columns[2].Width = 200; |
| radGridView1.Columns[3].Width = 50; |
| radGridView1.Width = 700; |
| radGridView1.MasterGridViewTemplate.Columns[0].HeaderText = "Id"; |
| radGridView1.MasterGridViewTemplate.Columns[0].ReadOnly = true; |
| radGridView1.MasterGridViewTemplate.Columns[1].HeaderText = "Name"; |
| radGridView1.MasterGridViewTemplate.Columns[1].ReadOnly = true; |
| radGridView1.MasterGridViewTemplate.Columns[2].HeaderText = "Description"; |
| radGridView1.MasterGridViewTemplate.Columns[2].ReadOnly = true; |
| radGridView1.MasterGridViewTemplate.Columns[3].HeaderText = "Order"; |
| radGridView1.MasterGridViewTemplate.AllowAddNewRow = false; |
| radGridView1.MasterGridViewTemplate.EnableGrouping = false; |
| } |
| private void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e) |
| { |
| MessageBox.Show("Start"); |
| IInputEditor editor = radGridView1.ActiveEditor; |
| if (editor.GetType() == typeof(RadComboBoxEditor)) |
| { |
| RadComboBoxElement comboElement = (RadComboBoxElement) ((RadComboBoxEditor)editor).EditorElement; |
| comboElement.SelectedIndexChanged += new EventHandler(comboElement_SelectedIndexChanged); |
| } |
| } |
| void comboElement_SelectedIndexChanged(object sender, EventArgs e) |
| { |
| int index = Convert.ToInt32(((RadComboBoxElement)sender).SelectedValue); |
| MessageBox.Show("Selected Index Changed. Value: " + index); |
| } |
| private void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e) |
| { |
| MessageBox.Show("Edit End"); |
| IInputEditor editor = radGridView1.ActiveEditor; |
| if (editor.GetType() == typeof(RadComboBoxEditor)) |
| { |
| RadComboBoxElement comboElement = (RadComboBoxElement)((RadComboBoxEditor)editor).EditorElement; |
| comboElement.SelectedIndexChanged -= new EventHandler(comboElement_SelectedIndexChanged); |
| } |
| } |
| } |
| } |
Thank you for the code it demonstrated the issue nicely. This is not a bug but merely a side effect which originates from the synchronization of RadListBoxElement inside RadComboBoxElement with the CurrencyManager object which keeps track of the selected position in the data source. In order to not get the previous index you need to assign a new BindingContext in the SelectedIndexChanged event. For example your SelectedIndexChanged event handler can be re-written:
void comboElement_SelectedIndexChanged(object sender, EventArgs e){ RadComboBoxElement combo = (RadComboBoxElement)sender; int index = combo.SelectedIndex; if (index == -1) { combo.ListBoxElement.BindingContext = new BindingContext(); }}Write again if you have other questions.
All the best,
Victor
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.