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
close icon

Setting SfDataGrid ViewModel data

Hi, I have a datagrid showing dynamic info from a custom object, the problem is that in the example of the filtering after setting the viewmodel there is this method 

public void SetRowstoGenerate(int count)
        {
            BookRepository bookrepository = new BookRepository();
            bookInfo = bookrepository.GetBookDetails(count);
        }  

the problem is that, I dont have the same repository as in the model, i use a class named PolizaElementoInfoRepository

public List<PolizaElementoBO> polizaElementos;
        private ObservableCollection<PolizaElementoInfo> polizaInfo;
        public ObservableCollection<PolizaElementoInfo> PolizaInfoCollection
        {
            get { return polizaInfo; }
            set { this.polizaInfo = value; }
        }

        public PolizaElementoInfoRepository(List<PolizaElementoBO> data)
        {
            this.polizaElementos = data;
            polizaInfo = new ObservableCollection<PolizaElementoInfo>();
            this.GeneratePolizaElementos(this.polizaElementos);
        }

        private void GeneratePolizaElementos(List<PolizaElementoBO> datos)
        {
            ProductoBM productoManager = new ProductoBM();
            foreach (var dato in datos)
            {
                if (dato.Texto1 == null)
                {
                    dato.Texto1 = "No";
                }
                if (dato.Texto2 == null)
                {
                    dato.Texto2 = "No";
                }
                if (dato.Texto3 == null)
                {
                    dato.Texto3 = "No";
                }
                var producto = productoManager.queryProductoWithId(dato.ProductoPrecio.IdProducto);
                polizaInfo.Add(new PolizaElementoInfo(producto.Clave, producto.Sku,  producto.Nombre, dato.Referencias, dato.Cantidad1,                  dato.Cantidad2, dato.Cantidad3, dato.Precio1, dato.Precio2, dato.Precio3, dato.Texto1, dato.Texto2, dato.Texto3));
            }
            
        } 

when in the filtering example the bookrepository is set hardcoded, is there any way I can adapt the way the data is loading?

thanks in advance for any answer

3 Replies

VP Vimal Prabhu Manohkaran Syncfusion Team October 27, 2016 07:08 PM UTC

Hi Arturo,

Thanks for contacting Syncfusion Support.

Unfortunately we are not able to clearly understand your query and requirement. Assuming your requirement is to set itemsource to the SfDataGrid and from the codes you have shared with us , refer the below code example to achieve it.

ViewModel.cs 
public class ViewModel() 
{

PolizaElementoInfoRepository collection;
 
public ViewModel() 
{ 
    collection = new PolizaElementoInfoRepository(data); 
    polizaInfo = collection.PolizaInfoCollection; 
} 
 
private ObservableCollection<PolizaElementoInfo> polizaInfo; 
public ObservableCollection<PolizaElementoInfo> PolizaInfoCollection 
{ 
    get { return polizaInfo; } 
    set { this.polizaInfo = value; } 
} 

Then set the ItemSource for the grid as follows,

SfGrid.ItemsSource = ViewModelObj.PolizaInfoCollection;

Else if your requirement is to apply filetering or is different from what we have understood please revert us with clear details so that we can help you better.

Regards,
Vimal Prabhu 



AC Arturo Calvo October 28, 2016 08:25 PM UTC

yeah, thats the way im setting the itemsource of the datagrid, but when I try to apply the filter in the examples the source is set in a different way than this, and  I dont understand how to implement the filter with the way im setting the item source


DS Divakar Subramaniam Syncfusion Team October 31, 2016 11:25 AM UTC

Hi Arturo, 
 
 
Thanks for the update. 
 
 
In SfDataGrid, you can able to filter the records in a view using SfDataGrid.View.Filter property where Filter is nothing but a callback set by the consumer to determine whether an item is suitable for inclusion. In order to display the filtered records in the view, it is necessary to assign the FilterRecords method to SfDataGrid.View.Filter predicate. Please refer the below code snippet to know how to set the filtered records to the predicate. 
 
//Filtering.cs 
internal void OnFilterChanged() 
{ 
    if (this.SfGrid.View != null) 
    { 
        //Here we have used a predicate called Filter in order to display the filtered records. 
        SfGrid.View.Filter = viewmodel.FilerRecords; 
        SfGrid.View.RefreshFilter(); 
    } 
} 
 
//ViewModel.cs 
 
//You can filter the records based your requirement 
public bool FilerRecords(object o) 
{ 
    double res; 
    bool checkNumeric = double.TryParse(FilterText, out res); 
    var item = o as PolizaElementoInfo; 
     
    if (item != null && FilterText.Equals("")) 
    { 
        return true; 
    } 
    else 
    { 
        if (item != null) 
        { 
            if (checkNumeric && !SelectedColumn.Equals("All Columns") && SelectedCondition != "Contains") 
            { 
                bool result = MakeNumericFilter(item, SelectedColumn, SelectedCondition); 
                return result; 
            } 
            else if (SelectedColumn.Equals("All Columns")) 
            { 
                if (item.BookID.ToString().ToLower().Contains(FilterText.ToLower())) 
                    return true; 
                return false; 
            } 
            else 
            { 
                bool result = MakeStringFilter(item, SelectedColumn, SelectedCondition); 
                return result; 
            } 
        } 
    } 
        return false; 
} 
 
 
 
Please refer the below UG link to know more about Filtering. 
 
 
 
We have prepared a simple sample by loaded single column and applied filtering for that column for your reference and you can download the same from the below location. 
 
 
 
In the sample, we have filtered the records based on some conditions. However, you can modify the conditions in the FilterRecords method based on your requirement. 
 
 
 
Regards, 
Divakar. 


Loader.
Live Chat Icon For mobile
Up arrow icon