Git Product home page Git Product logo

Comments (3)

Hangarspace avatar Hangarspace commented on May 28, 2024

I've been trying to implement a similar thing for the situation where I want to show fewer columns on a phone in portrait mode than in landscape mode when the device orientation changes. I'm sure the correct way to do this is using a datatemplateselector on the underlying listview control, but I'm not sure that would cause the current contents to be re-drawn.

Changing DataGrid.ColumnCollection causes the header cells to be updated fine and if you then disconnect and re-connect the underlying listview.itemsource it will redraw the list, which kind of does what we want. Although, if you have 'sorted' the list by clicking on one of the header cells, then the alternating row colors are all messed up!

Here is a very crude snippet of my experimental code...

`
public enum Layouts { L1, L2 }

    private Layouts _layout { get; set; } = Layouts.L1;
    private void SwapButton_Clicked(object sender, EventArgs e)
    {
        if (_layout == Layouts.L1)
        {
            this.dataGrid.Columns = GridLayout2();
            _layout = Layouts.L2;
        }
        else
        {
            this.dataGrid.Columns = GridLayout1();
            _layout = Layouts.L1;
        }

        Grid grid = this.dataGrid.Children[0] as Grid; // the header 

        ListView lv = this.dataGrid.Children[1] as ListView;     // the body of the datagrid

        lv.ItemsSource = null;

        lv.ItemsSource = Model.FilteredCatalogItems;

        
    }

    private Xamarin.Forms.DataGrid.ColumnCollection GridLayout1()
    {
        Xamarin.Forms.DataGrid.ColumnCollection cc = new Xamarin.Forms.DataGrid.ColumnCollection();

        cc.Add(new Xamarin.Forms.DataGrid.DataGridColumn() { Title = "Code", PropertyName = "ProductCode", Width = 100 });
        cc.Add(new Xamarin.Forms.DataGrid.DataGridColumn() { Title = "Description", PropertyName = "ProductDescription", Width = new GridLength(2, GridUnitType.Star) });
        cc.Add(new Xamarin.Forms.DataGrid.DataGridColumn() { Title = "Unit", PropertyName = "IssueUnit", Width = new GridLength(1, GridUnitType.Star) });
        cc.Add(new Xamarin.Forms.DataGrid.DataGridColumn() { Title = "Price", PropertyName = "Price", Width = new GridLength(1, GridUnitType.Star) });
        cc.Add(new Xamarin.Forms.DataGrid.DataGridColumn() { Title = "Quantity", PropertyName = "OrderedQty", Width = new GridLength(1, GridUnitType.Star) });

        return cc;
    }

    private Xamarin.Forms.DataGrid.ColumnCollection GridLayout2()
    {
        Xamarin.Forms.DataGrid.ColumnCollection cc = new Xamarin.Forms.DataGrid.ColumnCollection();
        cc.Add(new Xamarin.Forms.DataGrid.DataGridColumn() { Title = "Code", PropertyName = "ProductCode", Width = 100 });
        cc.Add(new Xamarin.Forms.DataGrid.DataGridColumn() { Title = "Description", PropertyName = "ProductDescription", Width = new GridLength(2, GridUnitType.Star) });
        cc.Add(new Xamarin.Forms.DataGrid.DataGridColumn() { Title = "Quantity", PropertyName = "OrderedQty", Width = new GridLength(1, GridUnitType.Star) });

        return cc;
    }`

Like I say, it kind of works depending on your situation but I'm sure it could be done better.

Chris

from xamarin.forms.datagrid.

Laradelacruz avatar Laradelacruz commented on May 28, 2024

Hi, I have a DataGrid and I want to freeze the first column so that it stays on the screen when you scroll it horizontally. Do you have an idea on how can I do that?

from xamarin.forms.datagrid.

Enigma86d avatar Enigma86d commented on May 28, 2024

Hi,

I have a similar issue, I need to programmatically change the number of columns being displayed.
I tried giving a new "ColumnCollection" and then updating "ItemsSource" and it seemed to work, however only on Android. If I try this on iOS some of the rows will show using the old ColumnCollection and some using the new ColumnCollection.
Is there a way to achieve this?
Thanks!

Marius

from xamarin.forms.datagrid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.