ComboBox - DropDownList won't Drop Down - c#

Here is my code:
public LayoutScheduler(){
InitializeComponent();
this.Load += (sender, args) =>
{
this.LoadLayouts();
};
}
public void LoadLayouts()
{
ImmutableSet<string> layoutNames = _store.Current.Keys;
layoutComboBox.BeginUpdate();
foreach (string name in layoutNames)
{
layoutComboBox.Items.Add(name);
}
layoutComboBox.EndUpdate();
layoutComboBox.SelectedIndex = 0;
}
I have this ComboBox set up in my designer to be a DropDownList style, however, in debug I can see the ComboBox Items list grow, when it displays, it'll display the first item as it's default, however I can't drop down the list.
If I then change the DropDownStyle to a simple, editable DropDown, and do the same I get the same behaviour, UNTIL I select the text in the drop down, at which point I am able to drop down the list.
I can't for the life of me figure out what's going on here. Any ideas?
EDIT: Here's the code for how this user control gets called and added to the form and displayed:
var layoutSchedulerControl = new LayoutScheduler(connected.Connection.Store, connected.Connection.Schedules);
Form layoutSchedulerForm = Statics.CreateForm("Layout Scheduler", layoutSchedulerControl);
layoutSchedulerForm.ShowDialog(this);
layoutSchedulerForm.Dispose();

Related

Don't display items to combobox from Entity Framework

I have defined a model in Entity Framework. Now I want to fill out the combobox from this model. But I was unsuccessful.
private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
{
using (SamenEntities c = new SamenEntities())
{
comboBox6.DataSource = c.sabt_como_tahsili.ToList();
comboBox6.ValueMember = "id_vaziat_tahsili";
comboBox6.DisplayMember = "name_vaziat_tahsili";
}
}
No data is displayed in the comboBox
You should fill your combo box when you initialize your form. Or maybe create a RefreshDataSources function which will reload every data set on your form, this combo box included. Like this maybe?
private void RefreshDataSources()
{
using (SamenEntities c = new SamenEntities())
{
#region combobox
comboBox6.DataSource = c.sabt_como_tahsili.ToList();
comboBox6.ValueMember = "id_vaziat_tahsili";
comboBox6.DisplayMember = "name_vaziat_tahsili";
#endregion
// place other controls here
}
}
But doing that in the event SelectedIndexChanged is not the best choice, even when it would work.
EDIT
Just checked the behavior of a usual microsoft combobox item.
The SelectedIndexChanged won't be launched if there is no items inside it, so no chance to execute your code.
public Form1()
{
InitializeComponent();
using (SamenEntities c = new SamenEntities())
{
comboBox6.DataSource = c.sabt_como_tahsili.ToList();
comboBox6.ValueMember = "id_vaziat_tahsili";
comboBox6.DisplayMember = "name_vaziat_tahsili";
}
}

How to Display items in data grid view based on many checkbox selection in wpf

I have developed a UI which looks as follows:
I want to display the items in the data grid on the basis of selected checkboxes.
Current Scenario:
Right now with my code always the items present in my latest checkbox selected file is getting displayed. For e.g. if i have selected 1st checkbox then the items present in first file is correctly displayed and similarly if i have selected 2nd checkbox then items from 2nd file is getting displayed.
Required Usecase (Not Able to do this)
If both the checkboxes are selected then items from both the files should be displayed. Secondly, if i deselct one checkbox then items from that file should not be displayed. I mean the items should be updated dynamically on the basis of checkbox selection.
My Try
FrameworkElementFactory FF = new FrameworkElementFactory(typeof(CheckBox));
FF.SetBinding(CheckBox.BindingGroupProperty, new Binding("Index"));
FF.SetBinding(CheckBox.IsCheckedProperty, new Binding("FileSelected") { UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
FF.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Center);
for (int i = 0; i < Resultnew.Count; i++)
{
var Dgrh = new DataGridRowHelper() { File_Name = lb1.Content, Index = i };
Dgrh.ThrowFileSlctdEvent += (sender1, args) => {items = PopulateVariables(Resultnew[Dgrh.File_Name.ToString()]); DataGrid.ItemsSource = items; };
Dgrh.ThrowFileNotSlctdEvent += (sender2, args) => { };
A2lFilesPopulateList.Items.Add(Dgrh);
}
Definitions
ObservableCollection<DataGridRowHelper> items = new ObservableCollection<DataGridRowHelper>();
private Dictionary<String, ParsedResult> Resultnew = new Dictionary<String, ParsedResult>();
public delegate void EventHandler(object sender, EventArgs args); //Defined in DataGridrowhelperClass
public event EventHandler ThrowFileSlctdEvent = delegate { }; //Defined in DataGridrowhelperClass
public event EventHandler ThrowFileNotSlctdEvent = delegate { }; //Defined in DataGridrowhelperClass
One Possible Solution
Make a "OK" button. After selecting checkboxes user have to click on OK button everytime. Send the selected checkbox as list and display the files accordingly. (I have implemented this successfully but i dont want this solution as it is not user friendly).
Please suggest me how to implement "Required UseCase"

Search from selected values of comboboxes windows forms C#

I have like 10 combobox in a form and what i need to do is if a item from any combobox (may be 1 or more comboboxes) is selected, search if theres a coincidence in an string array and the value(s) selected from the combobox(es) but exclude the comboboxes that have not been selected or changed, I tried using if but its not enough or efficient way to do this I think, I was just wondering if theres any way to do this.
You should be able to store the information on your class in a way that you can determine if the current ComboBox from the rest of the others. I cam up with a small sample that tracks the changes that have been made to the ComboBox using an event handler on SelectedIndexChanged and used LINQ in order to filter out and exclude the current ComboBox.
List<ComboBox> _allComboBoxes;
ComboBox _comboBox1;
ComboBox _comboBox2;
string[] _values;
void InitForm()
{
// TODO Implement values.
_values = new string[0];
_allComboBoxes = new List<ComboBox>();
_comboBox1 = new ComboBox();
_allComboBoxes.Add(_comboBox1);
_comboBox1.SelectedIndexChanged += ComboBoxSelectedIndexChanged;
_comboBox2 = new ComboBox();
_allComboBoxes.Add(_comboBox2);
_comboBox2.SelectedIndexChanged += ComboBoxSelectedIndexChanged;
}
void ComboBoxSelectedIndexChanged(object sender, EventArgs e)
{
if (HasCoincidence(sender as ComboBox, _values, _allComboBoxes))
{
throw new InvalidOperationException("Coincidence occurred.");
}
}
bool HasCoincidence(ComboBox comboBox, string[] values, IEnumerable<ComboBox> allComboBoxes)
{
IEnumerable<ComboBox> excludedComboBoxes = allComboBoxes.Where(c => c != comboBox);
throw new NotImplementedException("Implement ComboBox to string[] comparison.");
}

How can I filter dropdown values from another dropdown in WPF C#?

I am unable to filter the dropdown in WPF form like in c# form..
I created a list for my scenario dropdown and used it as items source in LoadUserControl() method as shown below.
public class FilterListItem
{
public string Filter { get; set; }
}
public LoadUserControl()
{
InitializeComponent();
List<FilterListItem> FilterList = new List<FilterListItem>();
FilterList.Add(new FilterListItem() { Filter = "Make" });
FilterList.Add(new FilterListItem() { Filter = "Model" });
FilterList.Add(new FilterListItem() { Filter = "Year" });
myscenario.ItemsSource= FilterList;
myscenario.SelectedItem= "Model";
}
Here's the code that i have for Script dropdown to filter as per the selection from scenarios dropdown.
private void myscenario_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (myscenarios.SelectedItem.ToString() == "Model")
{
Scriptlist.Add(script1);
Scriptlist.Add(script2);
break;
}
else if (myscenarios.SelectedItem.ToString() == "Make")
{
Scriptlist.Add(script3);
Scriptlist.Add(script4);
break;
}
else
{
Scenariolist.Add(sc5);
Scenariolist.Add(sc6);
}
}
When i execute the script, the UI is only going to default value which is model and whenever i select other value from dropdown, it still shows the selected item as "Model".
Also, When i debug for the value of the selected item from scenarios dropdown with the messagebox, i get this message as below.
**MessageBox.Show(myscenarios.SelectedItem.ToString());
//I get 'Filterlistitem' in message box instead of the selected item that i just selected.**
All i need is to filter the script dropdown as per the selection of the user in scenario dropdown like i mentioned above.
FYI, i didn't put the xaml UI just to make the question simpler.
Any help would be appreciated.
Thanks
seleniumlover
It appears that you are using List<T> as the ItemsSource of your dropdown control which does not update the UI when the list changes. Use ObservableCollections to notify the UI when the collection changes.

DataGridViewCombBoxColumn cell value and different dropdown list

I've a very trivial requirement which makes me go nuts. I've a DataGridView in windows forms application. This contains one databound ComboBox Column. I'm using DisplayMember and ValueMember properties of that combobox.
Now my requirement is ComboBox should show the list of DisplayMembers in drop down list but when user selects one item from it, I should display the part of that DisplayMember in the combobox cell visible to the user. For example.
My display member list looks as below:
"Cust1 - Customer 1"
"Cust2 - Customer 2"
"Cust3 - Customer 3"
and when user selects any one of them from the above list (Say user selected 'Cust2 - Customer 2') then I need to display the value in the combobox column cell as only "Cust2" instead of complete DisplayMember text.
This DisplayMember list is a combination of two fields from the datasource bound to it i.e. First part points to CustomerCode field and second part points Customer name. I need to display only CustomerCode in the ComboBox cell after user selects one item from the drop down list.
How can I do this? Or should I come up with my own control which will have a different AutoCompleteCustomSource and display member value. Even I'm confused with that approach too.
Update: As no one has come up with any solution to my problem. Now I'm starting a bounty for that, also if anyone can suggest me other way to implement the same functionality, it would be great.
I've even tried to come up with my own control and tried to work on simple combobox to display a different value than the selected dropdown list, even that didn't work. Is there any other way to implement this? Any tips and tricks are greatly appreciable.
#Anurag: Here is the code which I've used.
Created a datagridview in the design mode. Created one column of type 'DataGridViewComboBoxColumn' that and named it as CustomerColumn.
In the designer file it looks like below:
private System.Windows.Forms.DataGridViewComboBoxColumn CustomerColumn;
This is the entity class which I've used for datasource
public class Customer
{
public int Id { get; set; }
public string CustCode { get; set; }
public string CustName { get; set; }
public string NameWithCode { get; set; }// CustCode - CustName format
}
In the form load event I'm doing the following:
CustomerColumn.DataSource = GetCustomers();
CustomerColumn.DisplayMember = "NameWithCode";
CustomerColumn.ValueMember = "Id";
I'm answering my own question because I've implemented my own solution to this by using custom control.
This custom control is created by keeping a textbox above combo box in such a way that only drop down button of combobox is visible.
Now I've created a custom column in datagridview deriving the DataGridViewEditingControl from my usercontrol.
I've added a property in Usercontrol which will take drop down list source from the control which is hosting datagridview.
Now in the EditingControlShowing event I'm setting this property as below
private void dataGridView2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if(dataGridView2.CurrentCell.ColumnIndex.Equals(0) && e.Control is UserControl1)
{
var uscontrol = e.Control as UserControl1;
uscontrol.DropDownListSource = source;
}
}
This drop down list source is used in the usercontrol to set the autocompletesource to the textbox and datasource to the combobox as below:
Whenever I set the DropDownDataSource I'm firing an event in the usercontrol which will do the following. This is to ensure that every time EditingControlShowing event occurs for this column in DataGridView, this source is updated for textbox and combobox in usercontrol.
private void DropDownSourceChanged(object sender, EventArgs eventArgs)
{
textBox1.AutoCompleteCustomSource = DropDownListSource;
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
comboBox1.DataSource = DropDownListSource;
}
Now whenever user starts typing in the textbox autocomplete source will display dropdown list with 'NameWithCode' values and if user selects one of them then I'll set it to the Text propery overidden in my usercontrol which will be used for the cell value in the DataGridView. Now based on the Textbox text (which is NameWithCode) I can get the code part and set it to the text property.
If user uses combobox dropdown button to select the item then I'll get the combobox selected text and set it in the Textbox which is ultimately used by the cell for getting value.
This way I could achieve the solution I want.
#Homam, solution also works but when I change the ComboBox's DropDownStyle to allow the user to type the value in the combobox it behaves weirdly and not getting up to the mark solution for my requirement. Hence I used this solution.
I know that this is not perfect solution, but I looked for a better one and I didn't find, so I went to a workaround
I did the following:
when the user open the ComboBox, I change the DisplayMember to "NameWithCode"
when the user close it, I return it to "CustCode"
You can Access to the ComboBox control by DataGridView.EditingControlShowing event for the DataGridView.
The code:
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
var comboBox = e.Control as ComboBox;
comboBox.DropDown += (s1, e1) => comboBox.DisplayMember = "NameWithCode";
comboBox.DropDownClosed += (s2, e2) =>
{
// save the last selected item to return it after
// reassign the Display Member
var selectedItem = comboBox.SelectedItem;
comboBox.DisplayMember = "CustCode";
comboBox.SelectedItem = selectedItem;
};
}
Note: You have to start the DisplayMember with "CustCode"
Good luck!
Each time at the offensive of event dataGridView1_EditingControlShowing there is addition of new handlers for events comboBox.DropDown and comboBox.DropDownClosed. It results in the increase of number of these handlers and their repeated calls. This code decides this problem.
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
var comboBox = e.Control as ComboBox;
comboBox.DropDown += comboBox_DropDown;
comboBox.DropDownClosed += comboBox_DropDownClosed;
}
private void comboBox_DropDown(object sender, System.EventArgs e)
{
var comboBox = sender as ComboBox;
if(comboBox != null)
{
comboBox.DropDown -= comboBox_DropDown;
comboBox.DisplayMember = "NameWithCode";
}
}
private void comboBox_DropDownClosed(object sender, System.EventArgs e)
{
var comboBox = sender as ComboBox;
if(comboBox != null)
{
comboBox.DropDownClosed -= comboBox_DropDownClosed;
var selectedItem = comboBox.SelectedItem;
comboBox.DisplayMember = "CustCode";
comboBox.SelectedItem = selectedItem;
}
}

Categories

Resources