Xamarin.Forms.DataGrid icon indicating copy to clipboard operation
Xamarin.Forms.DataGrid copied to clipboard

Hide and freeze columns

Open pepobit opened this issue 8 years ago • 3 comments

How to add an IsVisible property for each column?

How to keep the left column always visible?

Is it possible?

pepobit avatar Nov 25 '17 12:11 pepobit

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

Hangarspace avatar Nov 26 '17 13:11 Hangarspace

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?

Laradelacruz avatar Sep 09 '20 07:09 Laradelacruz

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

Enigma86d avatar Jan 27 '22 08:01 Enigma86d