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.
Unfortunately, activation email could not send to your email. Please try again.

Setting SfDataGrid ViewModel data

Thread ID:

Created:

Updated:

Platform:

Replies:

127113 Oct 26,2016 02:35 PM Oct 31,2016 07:25 AM Xamarin.iOS 3
loading
Tags: SfDataGrid
Arturo Calvo
Asked On October 26, 2016 02:35 PM

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

Vimal Prabhu Manohkaran [Syncfusion]
Replied On October 27, 2016 03:08 PM

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 


Arturo Calvo
Replied On October 28, 2016 04:25 PM

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

Divakar Subramaniam [Syncfusion]
Replied On October 31, 2016 07:25 AM

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. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;