We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to keep selectedItem when lost focus

I Have a ComboDropDown which have PopupControl is a GridListControl

I have an event ComboDropDown _TextChanged when user search item , GridListControl.datasource will change

  private void ComboDropDown _TextChanged(object sender, EventArgs e)
        {
             lstHH = lst.Where().ToList();
            GridListControl.DataSource = lstHH;
            ComboDropDown .DroppedDown = true;
            if (GridListControl.Items.Count != 0)
            {
                GridListControl.SelectedItem = GridListControl.Items[0];
            }
        }
I have a problem when set SelectedItem , it can not be select !

How can i can set SelectedItem. Thank for helping !!!

3 Replies

PM Piruthiviraj Malaimelraj Syncfusion Team April 10, 2017 04:55 PM UTC

Hi Truong, 
 
Thanks for your interest in Syncfusion products. 
 
By default, the selection can be set when the control gets focus. So if you want to set the selected item in GridListControl, set the focus to the GridListControl.  
 
Code example: 
void comboDropDown1_TextChanged(object sender, EventArgs e) 
{ 
    this.gridListControl1.DataSource = countries; 
    this.comboDropDown1.DroppedDown = true; 
    if (this.gridListControl1.Items.Count != 0) 
    { 
        this.gridListControl1.Focus(); 
        this.gridListControl1.SelectedItem = this.gridListControl1.Items[0]; 
    } 
} 
 
Note: 
GridListControl can be coupled to a ComboBoxBase control directly and it allows to select the corresponding item based on combobox text value. Please refer to the below UG and DashBoard sample from the below location, 
 
Code example: 
this.comboBoxBase1.ListControl = this.gridListControl1; 
 
UG Document: 
 
DashBoard Sample location: 
<InstalledLocation\Syncfusion\EssentialStudio\VersionNumber\Windows\Grid.Windows\Samples\Grid List Control\Drop-Down Grid Demo> 
 
Regards, 
Piruthiviraj 



TR Truong April 14, 2017 07:30 AM UTC

I can not focus to GridListControl , i want to select the first row of GridListControl when Text value of ComboDropDown changed and do not focus to GridListControl,but it not working, the GridListControl item can not be selected.How a item of GridListControl be selected when the control not focus.I will try with ComboboxBase


PM Piruthiviraj Malaimelraj Syncfusion Team April 17, 2017 11:14 AM UTC

Hi Truong,   
 
Thanks for the update.   
 
By default, In GridListControl , the selected index will be changed based on text typed in ComboBoxDropDown which satisfies the DisplayMember value of GridListControl (Case-Senstive). This is the default behaviour. As per your scenario, you are trying to set the selection when combobox text is changed. So if typed text is same as first item(DisplayMember) of GridListControl, the selection will be automatically set.    
So if you want to set the selection on first item when typed any value on ComboBox, add the first row of GridListControl to Selections. Please make use of the below code example,   
 
Code example:   
void comboDropDown1_TextChanged(object sender, EventArgs e)   
{   
    this.gridListControl1.DataSource = countries;   
    this.comboDropDown1.DroppedDown = true;   
    if (this.gridListControl1.Items.Count != 0)   
    {   
        this.gridListControl1.Grid.Model.Selections.Add(GridRangeInfo.Row(1));   
    }   
}   
  
Sample link   
    
Note:   
If you want to get the selected item, use the selection range of that selected rows. Please refer to below code example,   
Code example:   
        //Get the selected item.   
        int row = this.gridListControl1.Grid.Model.Selections.Ranges[0].Top;   
        DataRowView selectedItem = this.gridListControl1.Items[row - 1] as DataRowView;  
 
 
Please let us know if you have any other queries,   
 
Regards,   
Piruthiviraj 

Loader.
Up arrow icon