My code is:
private void cmbMaritalStatus_Click(object sender, EventArgs e)
{
if (cmbMaritalStatus.BindingContext==null)
{
cmbMaritalStatus.Text = string.Empty;
FillComboboxes();
}
}
public void FillComboboxes()
{
cmbMaritalStatus.SetBindingToLookup(dataSource);
cmbMaritalStatus.BindSelected(bscAssistanceFileModel, pnr.Get(x => x.AssistanceFile.MaritalStatus));
}
the object dataSource is type of IEnumerable<CDX_MaritalStatus>
and I want that if this Combobox not was Binding so I send it to function
that bind it.
I try this code but in the first time it's looks good
But when that I debug it and I see that it's full, it's enter to the if
although that it's was Binding
The solution is only flag??
I donĀ“t know how you bind the data to the combobox. But you can try to check if the DataSource is null - like this:
DataSource source = cmbMaritalStatus.DataSource;
if (source.Count == 0)
{
cmbMaritalStatus.Text = string.Empty;
FillComboboxes();
}
Related
i have listview and textbox for search in listview every time user type in textbox i run new query, is there any better way to do that?without running query from database just from itemsources?
private void txtEditSearch_TextChanged(object sender, TextChangedEventArgs e)
{
if (txtEditSearch.Text != string.Empty)
{
var query = GetAllSchoolsAsync(txtEditSearch.Text);
query.Wait();
List<DataClass.Tables.School> data = query.Result;
if (data.Any())
dgv.ItemsSource = data;
}
else
getSchool();
}
i need something like this:
var basedata = dgv.Itemsource;
dgv.ItemSource = basedata.where(x=>x.Name == txtEditSearch.Text).Select(x=>x);
if the listview is populated with data you can filter on that data which acts just like a search, just displays the data you asked for. Here is the link I learnt about this from:
http://www.wpf-tutorial.com/listview-control/listview-filtering/
The best way, I recommend you, to achieve this is creating an initial unfiltered collection and filter from this collection.
Make a private field to store the initial School items:
private List<DataClass.Tables.School> _initialCollection;
Fill it with the unfiltered items in the contructor:
public MyView()
{
var query = GetAllSchoolsAsync();
query.Wait();
_initialCollection = query.Result;
}
In the TextChanged eventhandler you can add the filtering to theItemSource of the ListView:
private void txtEditSearch_TextChanged(object sender, TextChangedEventArgs e)
{
dgv.ItemSource = _initialCollection.Where(x=>x.Name == txtEditSearch.Text).Select(x=>x);
}
I tried a lot to get around this problem,
Basically, I'm checking if someone has the same country id as the one set in the combobox, if so, I add some information regarding them on the DataGridView below,
but It seemed to not work, I debuged the conditions and all, but I still didn't know what could be wrong with it,
public partial class Form_ListJ : Form
{
public Form_ListJ()
{
InitializeComponent();
comboBox_ListJ.DataSource = Form_Menu.dataSet.Tables["Pays"];
comboBox_ListJ.DisplayMember = "nomPays";
comboBox_ListJ.ValueMember = "idPays";
comboBox_ListJ.SelectedIndex = -1;
}
private void comboBox_ListJ_SelectedIndexChanged(object sender, EventArgs e)
{
dataGridView_ListJ.Rows.Clear();
foreach (DataRow dataRow_J in Form_Menu.dataSet.Tables["Joueur"].Rows)
{
if (dataRow_J[7] == comboBox_ListJ.SelectedValue)
dataGridView_ListJ.Rows.Add(dataRow_J[0], dataRow_J[1], dataRow_J[2]);
}
}
}
dataRow_J[7] is the field of 'idPays' inside of the 'Joueur' table.
try to convert values to string
like this
if (dataRow_J[0].ToString() == comboBox1.SelectedValue.ToString())
public void DD_Location()
{
var ctx = new LCCDB_EF();
var query = ctx.tbl_Location;
CB_Location.DataContext = query.ToList();
}
private void CB_Location_DropDownClosed(object sender, EventArgs e)
{
textbox_test.Text =CB_Location.Text;
}
Output in Textbox
System.Data.Entity.DynamicProxies.Location_5E43C6C196972BF0754973E48C9C941092D86818CD94005E9A759B70BF6E48E6
Try this
if(null != CB_Location.SelectedItem)
textbox_test.Text = CB_Location.SelectedItem.ToString();
Without seeing your XAML I can't be sure, but are you sure you've bound the list correctly? Try setting the Items property of combobox to your list, rather than the data context. Depending on what the type is and what you'd like to bind the text to, you may need to set the DisplayMemberPath property as appropriate, too.
I created a DataGrid and added a DataGridComboBoxColumn programmatically.
public partial class MainWindow : Window
{
private DataGridComboBoxColumn weightColumnChar = new DataGridComboBoxColumn();
ObservableCollection<int> mComboBoxValues;
public ObservableCollection<int> ComboBoxValues
{
get { return this.mComboBoxValues; }
set { this.mComboBoxValues = value; }
}//end property
public MainWindow()
{
InitializeComponent();
mComboBoxValues = new ObservableCollection<int>() {-1, 0, 1 };
}//end constructor
private void Window_Loaded(object sender, RoutedEventArgs e)
{
weightColumnChar.Header = "Weight";
dataGrid_Char.Columns.Add(weightColumnChar);
weightColumnChar.ItemsSource = ComboBoxValues;
Binding binding = new Binding();
binding.Path = new PropertyPath(ComboBoxValues[1]);
weightColumnChar.SelectedItemBinding = binding;
}//end method
private void dataGrid_Char_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}//end method
//Opens ComboBox on first click
private void dataGrid_Char_GotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() == typeof(DataGridCell))
{
DataGrid grd = (DataGrid)sender;
grd.BeginEdit(e);
}//end if
}//end method
}//end class
I added an ItemsSource to it and retrieve the values from an ObservableCollection.
The values from the collection are shown in runtime.
My problem is that if I select a value from the ComboBox this vlaue isn't selected and displayed afterwards. What am I doing wrong?
And I also want to select a default value. How does that work?
Please explain programmatically and not in XAML!
Would be great if anybody could help me.
Thanks!!!
First of all you haven't shown what your underlying collection is that is bound to the DataGrid.
You'll need something like DataGrid.ItemsSource = new ObservableCollection<MyClass>(); (it has to be a collection that supports change notification, thus I chose ObservableCollection).
Secondly you are binding the ComboBox.SelectedItemBinding to the ComboBox.ItemsSource which is nonsense. ComboBox.ItemsSource is the collection of values you can choose from, ComboBox.SelectedItemBinding (I would actually use ComboBox.SelectedValueBinding) is the "path" to the underlying data source that contains/reprsents the final value (eg. MyClass.IntProperty). So you select a value from a collection and assign that to some data item (you cannot assign it back to the collection where you select from).
PS! In case you later use something that resembles a KeyValuePair (eg. Id; Name) as your ComboBox.ItemsSource, then you set ComboBox.SelectedValuePath = Id; ComboBox.DisplayMemberPath = Name;. MyClass.IntProperty would contain the Id value in such a case.
This is really strange. I want to select a State and load Cities from that State in another combobox.
It's working EXCEPT when selecting the first item in the combobox:
Here's my entire class. The if statement in the selectedIndexChanged is to make sure that something is selected. The problem is that if I set that to cmbState.SelectedIndex >= 0 then an exception is raised because on initial load the comboBox doesn't has a .State variable there and not a .Value.
I don't know if this makes any sense.
private void MainForm_Load(object sender, EventArgs e)
{
LoadDepartmentsToComboBox();
}
private void LoadCitiesToComboBox(long StateID)
{
cmbCity.DataSource = null;
CityRepository cityRepo = new CityRepository();
cmbCity.DataSource = cityRepo.FindAllCities().Where(c => c.IDState == StateID);
cmbCity.DisplayMember = "Name";
cmbCity.ValueMember = "ID";
}
private void LoadDepartmentsToComboBox()
{
cmbState.DataSource = null;
StateRepository stateRepo = new StateRepository();
cmbState.DataSource = stateRepo.FindAllStates();
cmbState.DisplayMember = "Name";
cmbState.ValueMember = "ID";
}
private void cmbState_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbState.SelectedIndex > 0)
{
LoadCitiesToComboBox(Convert.ToInt64(cmbState.SelectedValue));
}
}
If I do use cmbState.SelectedIndex >= 0 then I receive this exception:
Unable to cast object of type
'DocumentScannerDanyly.State' to type
'System.IConvertible'.'System.IConvertible'.
When I don't use the SelectedIndex >= 0 and use plain old >0 then everything works except when selected the first item, which does nothing; understandably because it doesn't take the first item into account.
Thanks a lot for the help.
Don't assign the Display member & the Value member in each load, just assign them once in constructor for example.
add ToList() to the result which will assign to DataSource,
Complex DataBinding accepts as a data source either an IList or an IListSource.
check this.