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

RadDropDownList Selected Value

9 Answers 2065 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Роберт
Top achievements
Rank 1
Роберт asked on 21 Oct 2016, 09:02 AM

Hi,

Inthat scenario, when I try to edit the selected item, you'll see thelist.Selected Value is not changed.
But if you press button to get the Selected Value, you'll it's already = null.

It should keep the selected value, because it's needed to update data (text), using the id as value in a DB table..

public partial class RadForm2 : Telerik.WinControls.UI.RadForm
{
     
    RadDropDownList list = new RadDropDownList();
    TextBox tb = new TextBox();
    Button b = new Button();
 
    public RadForm2()
    {
        InitializeComponent();
        this.Controls.AddRange(new Control[] { list, tb, b });
 
        list.DropDownStyle = RadDropDownStyle.DropDown;
        list.Location = new Point(30, 30);
        list.Width = 100;
        list.Height = 25;
        list.DisplayMember = "name";
        list.ValueMember = "id";
        list.NullText = "not selected";
        list.SelectedValueChanged += list_SelectedValueChanged;
 
        tb.Location = new Point(30, 70);
        tb.Width = 100;
        tb.Height = 25;
 
        DataTable dt = new DataTable();
        dt.Columns.Add("id", typeof(int));
        dt.Columns.Add("name", typeof(string));
 
        dt.Rows.Add(1, "one");
        dt.Rows.Add(2, "two");
        dt.Rows.Add(3, "three");
        dt.Rows.Add(4, "four");
        dt.Rows.Add(5, "five");
        list.DataSource = dt;
 
        b.Location = new Point(30, 110);
        b.Width = 100;
        b.Height = 25;
        b.Text = "Get Selected Value";
        b.Click += b_Click;
    }
 
    void b_Click(object sender, EventArgs e)
    {
        tb.Text = list.SelectedValue + " <----> " + list.SelectedText;
    }
 
    void list_SelectedValueChanged(object sender, EventArgs e)
    {
        tb.Text = list.SelectedValue + " <----> " + list.SelectedText;
    }
}

 

9 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 21 Oct 2016, 09:28 AM
Hi,

Thank you for writing.

I have tested this with the latest version and the behavior is correct on my side. With which version are you testing this? I have attached a small video as well. 

I am looking forward to your reply.
 
Regards,
Dimitar
Telerik by Progress
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
0
Роберт
Top achievements
Rank 1
answered on 21 Oct 2016, 10:52 AM

Ok, what if I want to Update the Name of the Selected ID in a Database?

----> create table aTable (ID int, NAME varchar )

DataTable dt = .. select * from aTable

RDlist.DataSource = dt;

RDlist.ValueMember = name
RDlist.DisplayMember = name

If I need to update the name with UPDATE button, then I select the item and update its name.

Then I expect the SelectedValue is still the ID of the item.

And I just use update aTable set name=list.Text where id=SelectedValue

I have been doing that way for ages :)

In that scenario that you have, I must each time keep the previously selected value and use it when need to update.

0
Роберт
Top achievements
Rank 1
answered on 21 Oct 2016, 10:56 AM
When I want to add a new record, then I just don't mind what is in SelectedValue and add new record with new Name value.
0
Роберт
Top achievements
Rank 1
answered on 21 Oct 2016, 11:31 AM

sorry in previous RDlist.ValueMember = id

good if EDIT would be added to post div

0
Dimitar
Telerik team
answered on 24 Oct 2016, 08:18 AM
Hello Robert,

I still don't understand which is the exact behavior that you want to avoid. Could you please send me the code that you are using for the update and specify the exact moment where the index is not valid? 

Thank you in advance for your patience and cooperation. 

Regards,
Dimitar
Telerik by Progress
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
0
Роберт
Top achievements
Rank 1
answered on 23 Nov 2016, 09:48 AM

In case of when DataSource is a table lime I said with id&value you would consider the scenario when it used to update the data in table/
Let's say we have a simple table:

DIC_COMPANY
ID     |    NAME
=====================
11         Telerik Co.
12        Microsoft Inc.
13        ITBRIGADA Inc.

When such table is loaded, then the value is ID and the dataText - NAME

When a user selects one of the items, it supposed that the ID value in SelectedValue
Then user would like to rename the company name fromTelerik Co. to Telerik Corp.
If the SelectedValue contains its ID then it's easy to get all data to do the update DML command.
ID is in SelectedValue and the new name in Text..

Got My idea?

In 2012 it was like that, and it was perfect.

0
Роберт
Top achievements
Rank 1
answered on 23 Nov 2016, 10:37 AM

Just have a look.. what is most convenient way for a prigrammer, when SelectedValue is = id or NULL?

For me the SelectedValue is = id is obviously the best.

0
Accepted
Dimitar
Telerik team
answered on 23 Nov 2016, 12:52 PM
Hello Robert,

I understand your scenario now. You can just set the SyncSelectionWithText property and then you will get the correct index:
list.DropDownListElement.SyncSelectionWithText = false;

I hope this will be useful. 

Regards,
Dimitar
Telerik by Progress
Telerik UI for WinForms is ready for Visual Studio 2017 RC! Learn more.
0
Роберт
Top achievements
Rank 1
answered on 23 Nov 2016, 02:57 PM

Wow!!! Cool :) 

Lot of respect!!

Tags
General Discussions
Asked by
Роберт
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Роберт
Top achievements
Rank 1
Share this question
or