Hopefully I have missed something simple but I cannot seem to get SelectedValue to work in a databound list control.
The scenario is that I have a ListControl which has been filled from a DataTable with the DisplayMember set to a Name and the ValueMember set to a Database Id.
I programatically try to set the SelectedValue to a given Database Id, nothing happens, the selected value does not change, the ListControl selection point does not move, no events generated.
The various SearchString functions are useless to me because they work on the DisplayMember not the ValueMember and I would have duplicate names in my list.
I have tried writing my own routine to find the value manually and return an index, but I am also having problems with SelectedIndex not being set properly. This seems to work fine in simple scenarios but in a more complex scenario where the list is being changed and updated, it seems to stop working. As yet I cannot determine what is causing the problem with SelectedIndex but if I can get SelectedValue to work I do not have to worry about it.
I am using the Q1 2011 controls
Any feedback gratefully received.
Peter
24 Answers, 1 is accepted
I think that this recent forum post will probably answer your questions.
hope that helps
Richard
Sorry my experience directly contradicts that post (I checked it before writing this post), setting the SelectedValue as described in the post does not work at all for me and using the SearchString functions does not work for me as I need to search on the Value not the DisplayMember.
Regards
Peter
Here is a small sample (using the latest Q1 2011 release) for you to try in a new project using SelectedValue.
Designer File
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
.radListControl1 =
new
Telerik.WinControls.UI.RadListControl();
this
.radButton1 =
new
Telerik.WinControls.UI.RadButton();
this
.radSpinEditor1 =
new
Telerik.WinControls.UI.RadSpinEditor();
((System.ComponentModel.ISupportInitialize)(
this
.radListControl1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(
this
.radButton1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(
this
.radSpinEditor1)).BeginInit();
this
.SuspendLayout();
//
// radListControl1
//
this
.radListControl1.CaseSensitiveSort =
true
;
this
.radListControl1.ItemHeight = 18;
this
.radListControl1.Location =
new
System.Drawing.Point(13, 13);
this
.radListControl1.Name =
"radListControl1"
;
this
.radListControl1.Size =
new
System.Drawing.Size(259, 237);
this
.radListControl1.TabIndex = 0;
this
.radListControl1.Text =
"radListControl1"
;
//
// radButton1
//
this
.radButton1.Location =
new
System.Drawing.Point(133, 274);
this
.radButton1.Name =
"radButton1"
;
this
.radButton1.Size =
new
System.Drawing.Size(139, 20);
this
.radButton1.TabIndex = 1;
this
.radButton1.Text =
"Select Value"
;
this
.radButton1.Click +=
new
System.EventHandler(
this
.radButton1_Click);
//
// radSpinEditor1
//
this
.radSpinEditor1.Location =
new
System.Drawing.Point(13, 274);
this
.radSpinEditor1.Maximum =
new
decimal
(
new
int
[] {
10,
0,
0,
0});
this
.radSpinEditor1.Minimum =
new
decimal
(
new
int
[] {
1,
0,
0,
0});
this
.radSpinEditor1.Name =
"radSpinEditor1"
;
//
//
//
this
.radSpinEditor1.RootElement.AutoSizeMode = Telerik.WinControls.RadAutoSizeMode.WrapAroundChildren;
this
.radSpinEditor1.ShowBorder =
true
;
this
.radSpinEditor1.Size =
new
System.Drawing.Size(100, 20);
this
.radSpinEditor1.TabIndex = 2;
this
.radSpinEditor1.TabStop =
false
;
this
.radSpinEditor1.Value =
new
decimal
(
new
int
[] {
1,
0,
0,
0});
//
// Form1
//
this
.AutoScaleDimensions =
new
System.Drawing.SizeF(6F, 13F);
this
.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this
.ClientSize =
new
System.Drawing.Size(284, 320);
this
.Controls.Add(
this
.radSpinEditor1);
this
.Controls.Add(
this
.radButton1);
this
.Controls.Add(
this
.radListControl1);
this
.Name =
"Form1"
;
this
.Text =
"Form1"
;
((System.ComponentModel.ISupportInitialize)(
this
.radListControl1)).EndInit();
((System.ComponentModel.ISupportInitialize)(
this
.radButton1)).EndInit();
((System.ComponentModel.ISupportInitialize)(
this
.radSpinEditor1)).EndInit();
this
.ResumeLayout(
false
);
this
.PerformLayout();
}
#endregion
private
Telerik.WinControls.UI.RadListControl radListControl1;
private
Telerik.WinControls.UI.RadButton radButton1;
private
Telerik.WinControls.UI.RadSpinEditor radSpinEditor1;
}
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;
public
partial
class
Form1 : Form
{
private
int
InitialValue = 3;
public
Form1()
{
InitializeComponent();
this
.radListControl1.DataSource = GetTable();
this
.radListControl1.DisplayMember =
"Name"
;
this
.radListControl1.ValueMember =
"Id"
;
this
.radListControl1.SelectedValue = InitialValue;
}
static
DataTable GetTable()
{
DataTable table =
new
DataTable();
table.Columns.Add(
"Id"
,
typeof
(
int
));
table.Columns.Add(
"Name"
,
typeof
(
string
));
table.Rows.Add(1,
"Richard"
);
table.Rows.Add(2,
"Peter"
);
table.Rows.Add(3,
"Chris"
);
table.Rows.Add(4,
"Rob"
);
table.Rows.Add(5,
"Tom"
);
table.Rows.Add(6,
"Stewart"
);
table.Rows.Add(7,
"Leisl"
);
table.Rows.Add(8,
"Ester"
);
table.Rows.Add(9,
"John"
);
table.Rows.Add(10,
"Jacqui"
);
return
table;
}
private
void
radButton1_Click(
object
sender, EventArgs e)
{
this
.radListControl1.SelectedValue = Convert.ToInt32(
this
.radSpinEditor1.Value);
}
}
hope that helps
Richard
Thanks for taking the time to give me an example, although it did not solve my problem it did give me a working example to compare against my code to try and determine what was going wrong. First a quick description of what I am doing to set the scene (I am converting this application from winforms controls to Telerik):
I have a list of items (the ListControl), when you select an item you are able to edit the item details in the form. If you select another item, any changes to the current item are updated before the new selection is made. Because the item details could also include the name being displayed in the list, the list is redrawn from the database and the new item the user selected is selected in the re-drawn list.
The list redraw happens within the SelectedIndexChanged event and it is in this event that I try and set the selected value to the one the user has just clicked on before the list was re-drawn. This works perfectly in the winforms ListControl but it would seem that you cannot set either the SelectedValue or the SelectedIndex in the RadListControl while you are in a selected event, it does not return an error it just does not do anything.
Now I know this, I can work around it and do the update it in a different way that does not involve setting the SelectedValue within the event.
Thanks again
Peter
If you could post a sample that demonstrates the issue, I'll be happy to take a look at it for you.
Regards,
Richard
Yes, we found a similar issue related with the SelectedIndex/SelectedValue properties. However, we are not sure whether this is the same issue. Could you, please create a small sample project where the issue can be reproduced and send it to us? This will help me to investigate the case in detail and ensure that any potential issue will be addressed in our upcoming service pack. Thank you in advance.
I am looking forward to your reply.
All the best,
Jack
the Telerik team
We have found some problem with SelectedValue property when using in DataBinding Mode
I believe problem with BindingSource.
When control loading with DataSource like table.Columns("id","name")
And DisplayMember is name and ValueMember - id
Then OnForm Load it seems like RadDropDownList control looses its BindingSource, but DataSource is still valid.
So, when I set SelectedValue to any valid number referenced to its name, then RadDropDown behaves like it tries to set the value and change all other properties' values (SelectedIndex and Text and such), it takes some time (about several handreds milliseconds) to try, but then it still comes back to null values for all of them.
Maybe the bindingSource is locked by some Telerik internal service?
And the SelectedIndex in that case is equal to -1, even I don't set it to -1 After I set it manually to any valid number (say 0), the control initializes other properties, but dataBinding is failed anyway.
So, it is bad, as two-ways binding is very common in case if you have DataSource like table.Columns("id","name")
I tried to recreate the same problem on very simple project, and it worked ok, but in larger project it doesn't.
I could give you Remote Access to my PC, and you would try to see the issue, just drop me message to my email and time window when you're able to login.
{
try {
this.chop.DataBindings.Add("Text", bs[0], "DutyIssuer", false, DataSourceUpdateMode.OnPropertyChanged);
} catch (Exception ex) { MessageBox.Show(ex.Message); }
try {
this.chop.DataBindings.Add("SelectedValue", bs[0], "ChopId", false, DataSourceUpdateMode.OnPropertyChanged);
} catch (Exception ex) { MessageBox.Show(ex.Message); }
try {
this.dutyDate.DataBindings.Add("Value", bs[0], "DutyDate", false, DataSourceUpdateMode.OnPropertyChanged);
} catch (Exception ex) { MessageBox.Show(ex.Message); }
}
private void DutyPlace_Load(object sender, EventArgs e)
{
Domains.DutyPlace _d = (Domains.DutyPlace)d.Clone();
chop.SelectedIndex = 0;
bs.Clear();
chop.DataBindings.Clear();
dutyDate.DataBindings.Clear();
bs.DataSource = typeof(Domains.DutyPlace);
bs.Add(_d);
d = _d;
Bind();
}
chop - is RadDropDownList control.
I even tried to clear all bindings OnLoad Form without success. SelectedValue is set to null afterall.
Tried to change it from VS Debugger EditValue menu, it still keeps null value the way I described above.
Help!
NotifyPropertyChanged("Id"); isn't served correctly.
The SelectedValue, SelectedIndex and SelectedText Data Binding - is not implemented.
Thank you for writing.
I was not able to reproduce this issue using a sample project. I attached my test project here. I suppose that your business object does not implement the INotifyPropertyChanged interface. Please, could you confirm this? Implementing this interface is a prerequisite in this scenario.
I am looking forward to your reply.
Peter
the Telerik team
The DataBinding is driving me really mad.
Peter, do you know how to bind SelectedValue, SelectedIndex and Text properties all at once to a binding object.
Something like the following:
radDropDown1.DataBindings.Add("Text", bs, "MyObjectText", false, DataSourceUpdateMode.OnPropertyChanged);
radDropDown1.DataBindings.Add("SelectedValue", bs, "MyObjectValue", false, DataSourceUpdateMode.OnPropertyChanged);
radDropDown1.DataBindings.Add("SelectedIndex", bs, "MyObjectIndex", false, DataSourceUpdateMode.OnPropertyChanged);
and
public class MyObject :INotify...
{
protected string text;
public string MyObjectText {
get { return text; }
set {
text = value;
NotifyPropertyChanged("MyObjectText"); }
}
protected int myvalue;
public int MyObjectValue {
get { return myvalue; }
set {
myvalue = value;
NotifyPropertyChanged("MyObjectValue "); }
}
protected int myindx;
public string MyObjectIndex {
get { return myindx; }
set {
myindx= value;
NotifyPropertyChanged("MyObjectIndex"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
I tried to make 2 of the properties passive by commenting off NotifyPropertyChanged("MyObjectIndex"); without success.
It works unpredictable.
I can't make this construction workable.
The alternative is to implement the schema manually by serving OnChange.... in code and assigning the property values manually.
Help.
FormLoad Callback is done and the Form is not visualized yet, but the changing SelectedValue bound property in Data Source object at that stage doesn't make any changes on the control.
But if SelectedIndex is used instead of SelectedValue - then it works ok.
When the form, where the control is, loaded and Visualized, then binding works ok.
Seems like RadDropDownList control is able to be manipulated completely, only after it's loaded and Visualized in the application.
If it's not visble, then it's not 100% correct in acting with data binding on SelectedValue.
Thank you for sending me your code. Now I understand the issue. It appears that the Text property of RadDropDownList does not update properly via data binding. I added the issue in our bug tracking system and it we will address it in one of our upcoming releases.
I have updated your Telerik points for bringing our attention on this.
Should you have any other questions, do not hesitate to contact us.
Kind regards,
Peter
the Telerik team
A workaround is to set the bindingsource's position to the desired id (after binding the control)
Sample
Dim key As Int16 = AudioMeterBindingSource.Find("equipmentid", screeningaudioID)
AudioMeterBindingSource.Position = key 'do not use selected value property of control
Armand
I spent whole week to find a workaround without success.
The best workaround is to get back to old fashion style - proceed with the native callbacks - OnIndexChanged and such.
Even though it looks ugly in 21 Century, but .NET Currency Manager and ContextManager are dealing with the same native callbacks as well.
It is the fastest solution in ya case, otherwise waiting for the upcomming bug fixes will stop ya work for ages.
This bug behaves not stable.
P.S. I beleive that ListControl and DropDownList are written by the same scenario in the part of data binding.
Vitali, thank you for sharing this information.
Armand, I can confirm that there is not an easy workaround and the best way to resolve this for now is to proceed with the DropDown events, for example SelectedIndexChanged and SelectedValueChanged events.
Do not hesitate to contact us if you have further questions.
Peter
the Telerik team
Hi,
I am experiencing the same issue as described by Vitali. That is, I have a RadDropDownListElement which has a DataTable as a DataSource. I have set the DisplayMember and ValueMember. When I add the following databinding (the class for "batch" implements INotifyPropertyChanged):
periodComboBox.DataBindings.Add(
"SelectedValue"
, batch,
"PeriodId"
,
true
, DataSourceUpdateMode.OnPropertyChanged);
the dropDownList displays the correct value of batch.PeriodId at runtime. However, if I change the value of the combobox through the UI, the SelectedIndexChanging, SelectedIndexChanged and SelectedValueChanged events are fired, but the batch.PeriodId property is never set.
I tried putting the code for setting up the databindings in the form_Shown event to see if it was because of the timing of when the bindings are created, but the same behaviour occurred.
So is this a bug that still exists and I have to use the workaround suggested by Vitali? Or am I missing something?
Thanks.
Thank you for writing.
Following the provided information I have tried to reproduce the issue you are facing with the latest version but without any success. When the RadDropDownList selection is changed the bound property for the custom class is updated respectively. Please have a look at the attached gif file illustrating the behavior on my end. Am I missing something? I have attached my sample project. Could you please specify the exact steps how to reproduce the problem so I can investigate the precise case? Thank you in advance.
Alternatively, you can open a support ticket providing your project replicating the issue. Thus, we can make analysis of the specific case and assist you further. Thank you in advance. I am looking forward to your reply.
Regards,
Dess
Telerik
Hi Dess,
Thanks for the reply and the sample code. I see that your sample code works, and it is similar to mine, except that I am using a RadDropDownListElement object and you are using a RadDropDownList object.
FYI, mine was added through the designer UI to a RadRibbonBarButtonGroup. This RadRibbonBarButtonGroup is contained by another RadRibbonBarButtonGroup, which in turn is contained by a RadRibbonBarGroup, and finally contained by a RadRibbonBar.
So it seems the behaviour of a RadDropDownList is different than a RadDropDownListElement, at least as far as I can see.
Thanks,
Eric.
Hi Dess,
Thanks for your reply and sample code. I see that your code works, and it is similar to mine, except I am using a RadDropDownListElement and you are using a RadDropDownList.
FYI, mine was added through the designer, and is contained by a RadRibbonBarButtonGroup. This is subsequently contained by another RadRibbonBarButtonGroup, then a RadRibbonBarGroup, then a RadRibbonBar.
So it seems the behaviour of the RadDropDownList is different than a RadDropDownListElement, at least as far as I can tell.
Thanks,
Eric.
Thank you for writing back.
The provided detailed information is greatly appreciated. I have logged it in our feedback portal. You can track its progress, subscribe for status changes and add your vote/comment to it on the following link - feedback item.
I have also updated your Telerik points.
The possible solution that I can suggest is to use a RadHostItem and use RadDropDownList:
RadDropDownList ddl =
new
RadDropDownList();
ddl.MinimumSize =
new
System.Drawing.Size(100, 20);
RadHostItem host =
new
RadHostItem(ddl);
host.MinSize =
new
Size(100, 20);
this
.radRibbonBarButtonGroup2.Items.Add(host);
ddl.DataSource = dt;
ddl.DisplayMember =
"Name"
;
ddl.ValueMember =
"Id"
;
ddl.DataBindings.Add(
"SelectedValue"
, student,
"Id"
,
true
, DataSourceUpdateMode.OnPropertyChanged);
I hope this information helps. If you have any additional questions, please let me know.
Dess
Telerik
I am experiencing a problem that seems VERY related to this one. I originally discovered this while chasing down binding and datasource theories on this problem. I have now isolated it to the inability to write to the SelectedValue for the radDDL.
private void radGridView1_RowsChanged(object sender, Telerik.WinControls.UI.GridViewCollectionChangedEventArgs e)
{
var tsd = (t_Estimates)this.t_EstimatesBindingSource.Current;
if (tsd != null) { ServiceTyperadDropDownList.SelectedValue = tsd.ServiceType.Value; }
}
I have confirmed, that when the IF Conditional is executed, tsd.ServiceType.Value = 5. After exiting, ServiceTyperadDropDownList.SelectedValue = null.
I have several other radDDLs on the same form that work fine. It is only this one. I compared them, and could not identify any differences. Additionally, this appears to only be a problem with the 1st record in the t_EstimatesBindingSource Entity dataset.
Please advise.
-Scott
even in some code parts I get the value in demand index in the table and set it as setIndex.. then selected value is set :)
Thank you for writing.
The referred feedback item is related to using simple data binding. Setting directly the RadDropDownList.SelectedValue property is supposed to work as expected if the set value is correct considering the RadDropDownList.ValueMember. I have prepared a sample project for your reference. The selection in RadDropDownList is successfully changed when selecting a new row in te grid.
I hope this information helps. Should you have further questions I would be glad to help.
Regards,
Dess
Telerik by Progress