How can I bind my gridview to a list of objects - winforms

Iam using Devexpress winForms third party and I want to bind my datagridview to list so whenever the list changes it is reflected on UI

first you must declare a list of type Bindinglist.Then
BindingList<yourObject> bindingList1 = new BindingList<yourObject>();
gridview1.Datasource = bindingList1;
Make sure your have INotify for each property of your object

Related

Two ListViews with different filters on the same data set

I have a ViewModel with some ObservableCollection _questions, which is loaded from DB when VM instance created. Also this list is used to collect data to save back to DB.
This Vm is used for a View1 and it displays the list in ListView with filtering by a property using CollectionViewSource.GetDefaultView(_questions).Filter = ...
Now I need to create View2 which will display same list but without filtering.
I can't bind it to the same ObservableCollection _questions because it has filter defined on CollectionViewSource, but I need to use it to keep SaveToDb code same.
Is it possible to have different filtering on the same data source for two different ListViews?
I have never enjoyed using CollectionViewSource. I would instead filter using a new property in my ViewModel that filters using Linq:
public IEnumerable<MyType> FilteredItems()
{
get
{
return MyCollection.Where(x => x.MyProperty == SomeValue).ToArray;
}
}
I would then bind ItemsSource to this property and use INotifyPropertyChanged event to notify UI of changes to the collection.
Its hard to tell if this fits your scenario well as there is not much information provided on what you need to achieve.

How to rebind global declare list in App.xaml.cs in silverlight

i have list of custom class declare globally in App.xaml.cs and binding on first button click , in this way its working fine , but on second button click i want to clear list items and rebind new items, but its showing previous bind elements ,
i tried to clear first before binding but it shows nothing(i think its initialize only once for an application)
DeptWiseidAndName this list is declared public in App.xaml.cs
List EditList = new List();
EditList = App_Object.DeptWiseidAndName;
App_Object.DeptWiseidAndName.Clear();
plz help
Try to use an ObservableCollection instead of List. ObservableCollection alerts changes to its contents to object which bind to it.

Auto-Update a listbox bind with a Database without refreshing the page in Silverlight ...?

I have a list box in silvelight bind with a Observable collection wit is filled form a database , like this :
var item = new AllCommentsPerMaterialCategoryItem { CommenterName = name,
Text = project.Text, ID = project.ID, NoLike = (int)project.NoLike, SID = l, PID = i,
VID = (int)project.MID, Date = project.date.ToString() };
=_viewModel.AllCommentsPerMaterialCategoryItem.Add(item);= }
ObservableCollection<AllCommentsPerMaterialCategoryItem> GenreList =
_viewModel.AllCommentsPerMaterialCategoryItem; //result from a call to a WCF
service which returns the observable collection
GenreList1 = _viewModel.AllCommentsPerMaterialCategoryItem;
mainMenuList.ItemsSource = GenreList;
I want whenever someone adds a new item in the database to be automatically filled(updated-refreshed) in list box and also whenever I add a new item to be added to the list
I read that the Observable collection would internally implement "INoifyProbertyChange"
but its not working , also I'm not sure if that would do these requirements or not , i guess it would the second only.
any ideas please ?
ObservableCollection does indeed internally implement INotifyPropertyChanged. However, it's not going to go to the database for you. You need to either refresh from the database on a timer or set up a duplex service to notify you, and then add/remove items from the ObservableCollection as necessary.
The best usage of this would be to create a property returning an instance of the ObservableCollection and bind to it, then simply modify the collection as things change. If you're going to swap out the entire collection you'll need to make sure that the property itself notifies of the property changed as there's an important distinction between the ObservableCollection notifying the UI that its collection has changed and notifying that the variable that stored it changed to a different collection. The latter there doesn't happen automatically.

After adding a new row why aren't my bound controls updating?

I'm using wpf drag and drop databinding to datasets. I generate a new datatable row using the AddNew method of the BindingListCollectionView. I set values on the new row and call CommitNew on the BindingListCollectionView. I expect to see my assigned values in the bound controls but they are all blank. If I save the changes to the database the controls are then updated, but I want the user to see my assigned values before calling UpdateAll on the TableAdapterManager.
Background:
I've created a strongly typed dataset in a project separate from my wpf application. My wpf app references the dataset application. I added an object datasource in the wpf app pointing to the typed dataset. I dragged fields/controls from the datasources window to the wpf designer window. The generated xaml includes a Window.Resources section with a CollectionViewSource accurately bound to my dataset and datatable. This CollectionViewSource is the DataContext for the controls that I dragged to the design surface. All of the controls use TwoWay databinding.
When the window loads I grab a reference to the xaml's CollectionViewSource (using FindResource). I then grab a reference to the view property of the CollectionViewSource and cast it to a BindingListCollectionView. Now I use the AddNew method of the BindingListCollectionView to generate a new row (which AddNew returns as an object). I cast the object to a DataRowView and access it's Row property. I then cast the row to a strongly typed datatable row (generated by the DataSet designer). Now I assign values to some of the datatable row columns. I call CommitNew on the BindingListCollectionView. Finally I call MoveCurrentToFirst on the CollectionViewSource.
Problem:
Using a watch expression I can see the data is in the SourceCollection of both the CollectionView and the BindingListCollectionView. Can anyone explain why the bound controls do not show the data unless I save the changes to the database?
Code (generated XAML not shown):
Private WithEvents _cvsScanData As System.Windows.Data.CollectionViewSource
Private WithEvents _blcvScanData As System.Windows.Data.BindingListCollectionView
_cvsScanData = CType(Me.FindResource("Dt_tblScanDataViewSource"), System.Windows.Data.CollectionViewSource)
_blcvScanData = CType(_cvsScanData.View, BindingListCollectionView)
Dim newRow As LabDataSet.dt_tblScanDataRow = CType(CType(_blcvScanData.AddNew, System.Data.DataRowView).Row, LabDataSet.dt_tblScanDataRow)
newRow.SampleID = "testSampleID"
newRow.MachineID = "testMachineID"
_blcvScanData.CommitNew()
_cvsScanData.View.MoveCurrentToFirst()
The simple fix is to call the Refresh method of the BindingListCollectionView after calling CommitNew.
_blcvScanData.Refresh()
I stumbled across this answer to my own question via intellisense. If anyone can explain why refresh is necessary I'd appreciate it. I expected the INotifyPropertyChange interface to update the bound controls obviating the need to call refresh.

Silverlight listbox won't update with new itemsource

I have a listbox which is bound to a generic list, whenever I removed an item from the generic list and rebind it to the listbox it still shows the deleted items. Here is the code :
InventoryList.Remove(currInv);
lstSubMenu.ItemsSource = InventoryList;
lstSubMenu.DisplayMemberPath = "InventoryItemName";
I checked the generic list and the item is being removed and there doesn't seem to be any errors in the output window.
Set the ItemsSource = null before you set it to be InventoryList.
However, it is generally better practice to set the ItemsSource property once and never again. You can do this by using an ObservableCollection. Once you do this you can add/remove to your heart's content and not have to worry about the binding target not getting updated.

Resources