I am having issue with resetting my databound combobox to cmb.SelectedIndex = -1
Basically as per the title, the combo box is bound to a datasource and I have a reset button click event which sets the combox box selected index to -1. Main aim is to set the resultant to null.
However when I click the reset button it goes to the first item and then I have to click the button again which then sets the index to -1. So it seems like it goes to selectedIndex 1 and then -1? I am confused?
Here is the code
public SetupBindings()
{
cmb.DataBindings.Add(nameof(ComboBox.SelectedValue), DataSource,
nameof(DataSource.ID), true, DataSourceUpdateMode.OnPropertyChanged);
}
public void Reset_Click(object sender, EventArgs e)
{
cmb.SelectedIndex = -1;
}
Related
I have a simple Windows Form which contains a list box called lstVersenyzok. I have a lstVersenyzok_SelectedIndexChanged function. How can I clear the selection of the list box when I click the empty field of the list box? I tried, but it does not work if I check the condition lstVersenyzok.SelectedIndex == -1.
You can use MouseClick event and get the selected index by the IndexFromPoint method.
Check if the index is -1, then call the lstVersenyzok.ClearSelected() to clear the selection.
private void lstVersenyzok_MouseClick(object sender, MouseEventArgs e)
{
int index = this.lstVersenyzok.IndexFromPoint(e.Location);
if(index == -1)
{
lstVersenyzok.ClearSelected();
}
}
Hope this help !!
i have only one row in my grid, when i click on a gridrow one particular cell value is stored into textbox. I have a button, when click on button that textbox value is cleared. After i clear the textbox i click again on the same gridrow but its not sending any value to textbox.
`private void DataGrid1_Selectionchanged(object sender,SelectionChangedEventArgse)
{
var selectedRow = DataGrid1.SelectedItem;
TextBox1.Text = selectedRow.coloumnName.ToString();
}
private void btn_Click(object sender, RoutedEventArgs e)
{
TextBox1.Text = "";``
}`
Edit your button's CommandName property and set it to 'Select'
Or enable auto generate select button of the GridView.
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.autogenerateselectbutton(v=vs.110).aspx
The selection changed handler is not firing because you have one row. The first time works but the second time there is nothing to change to. Change the handler or add a second row
I have a ComboBox which is populated on Page Load event. Just after populating the ComboBox I call another method which returns a value that I want to make the default value of the ComboBox when Page loads. How can I change the selectedIndex to a value that is returned when I call another method?
XAML for ComboBox
<ComboBox Name="cboProductType" DisplayMemberPath="ProductTypeName" SelectedValuePath="ProductTypeID" SelectedIndex="0"/>
Page Load event:
void OnProductDetailLoad(object sender, RoutedEventArgs e)
{
GetServiceReference.Service1Client service = new GetServiceReference.Service1Client();
service.GetProductDetailsCompleted += service_GetProductDetailsCompleted;
service.GetProductTypeCompleted += service_GetProductTypeCompleted;
service.GetProductTypeAsync();
service.GetProductDetailsAsync(ProductId);
}
Populating ComboBox when Page Loads:
void service_GetProductTypeCompleted(object sender, GetProductTypeCompletedEventArgs e)
{
cboProductType.ItemsSource = e.Result;
}
Calling the other method which returns a particular ProductTypeName. I tried to get the index of that particular ProductTypeName but returns -1 always.
void service_GetProductDetailsCompleted(object sender, GetServiceReference.GetProductDetailsCompletedEventArgs e)
{
if (e.Result.Count != 0)
{
p.ProductID = e.Result[0].ProductID;
int index = cboProductType.Items.IndexOf(e.Result[0].ProductTypeName);
cboProductType.SelectedIndex = index;
}
Also is this a right approach for setting the SelectedIndex property?
Say I have following items loaded in ComboBox in following Index order:
Index DisplayMemberName(ProductTypeName)
0 Color
1 Size
2 Variant
3 N-size
and e.Result[0].ProductTypeName contains Variant so I now want SelectedIndex = 2 of ComboBox
I hope my question is clear
The SelectedIndex is only valid when you manually fill a combobox. If you are using the ItemsSource to fill the items in a combobox, you must define a SelectedValuePath which will then allows you to use the SelectedItem property to SET/GET the selected item. When using binding to a DataContext to bind the controls, the SelectedValuePath should be the property that links the Parent/Child together.
i have 2 listBoxes in a window, one next to the other, with buttons to copy items from one listBox to the other.
when an item from the first listBox is selected the copy button gets enabled, and remove button gets disabled. when i choose an item for the second listBox the copy button gets disabled, and remove button gets enabled.
when you select an item in one of the listBoxes the buttons change with no problem, after the listBox lost focus and you choose the same item that was selected the buttons dont change back.
i understand the problem is that the event of selected item changed will not fire, beacuse the selected item did not change.
setting the selected item to null when the listBox loses focus was not usefull beacuse i need the selcted item. i need to find a way to reselect the selected item when the listBox gains focus, or just fire the even of selected item changed. any suggestions?
You can try the ListBox.LostFocus Event and set the SelectedItem Property to null.
private void ListBox_LostFocus(object sender, RoutedEventArgs e)
{
((ListBox)sender).SelectedItem = null;
}
Use the ListBox.GotFocus event check if there is a SelectedItem, store the index, remove the SelectedItem and use the stored index to reset the SelectedItem. Something like this
private void ListBox_GotFocus(object sender, RoutedEventArgs e)
{
ListBox lb = (ListBox)sender;
if(lb.SelectedItem != null )
{
int index = lb.SelectedIndex;
lb.SelectedItem = null;
lb.SelectedIndex = index;
}
}
Whenever we add data source to combobox with code below.
comboBoxBusNo.DataSource = busNo.Tables[0];
comboBoxBusNo.DisplayMember = "BusId";
comboBoxBusNo.ValueMember = "Id";
Why private void comboBoxBusNo_SelectedIndexChanged(object sender, EventArgs e) event is called. How we can stop this to be called at loading.
When you bind a list to a ComboBox, the first item is selected by default, which sets the SelectedIndex to 0. There are no items prior to binding so no item is selected, thus the SelectedIndex is -1 to begin with. The SelectedIndex changes from -1 to 0 so the SelectedIndexChanged event is raised.
This will stop the event from firing on load.
comboBoxBusNo.SelectedIndexChanged -= comboBoxBusNo_SelectedIndexChanged;
comboBoxBusNo.DataSource = busNo.Tables[0];
comboBoxBusNo.DisplayMember = "BusId";
comboBoxBusNo.ValueMember = "Id";
comboBoxBusNo.SelectedIndexChanged += comboBoxBusNo_SelectedIndexChanged;