Repository: web2py/web2py

Issue:
If a grid is given fields subset and order, its "CSV (hidden cols)" export would contain mismatching headers and content
**Describe the bug**

If a grid is defined with an explicit `fields` parameter, which defines a subset of columns and in a customized order, its "CSV (hidden cols)" button would export a csv file containing a header following the order defined by `fields` FOLLOWED BY the rest of the fields, but the csv rows content are all in the order of fields defined by model. Hence a mismatch.

**To Reproduce**
Steps to reproduce the behavior:

1. Define a model

```python
db.define_table("my_table",
    Field("f1", "string"),
    Field("f2", "string"),
    Field("f3", "string"),
    Field("f4", "string"),
    Field("f5", "string"),
    )
```

2. Define a controller

```python
def foo():
    grid = SQLFORM.grid(db.my_table, fields=[db.my_table.f3, db.my_table.f2])
    return {"": grid}
```
Visit this controller, you will see only row `f3` and `f2`, in such order, as expected.
You can also create a record, and input values for all 5 fields, as expected. Let's say your record contains these 5 values: "v1", "v2", "v3", "v4", "v5", respectively.

3. Now click the button "CSV (hidden rows)", the csv file you get would currently look like this (spaces added below for readability), i.e. the headers and content are in different order:

```
my_table.f3, my_table.f2, my_table.f1, my_table.f4, my_table.f5
v1, v2, v3, v4, v5
```

**Expected behavior**

The csv output should ideally be:

```
my_table.f3, my_table.f2, my_table.f1, my_table.f4, my_table.f5
v3, v2, v1, v4, v5
```
or at the very least be:
```
my_table.f1, my_table.f2, my_table.f3, my_table.f4, my_table.f5
v1, v2, v3, v4, v5
```


**Screenshots**
N/A.

**Desktop (please complete the following information):**
I don't think its platform-dependent but I'm providing the following info anyway.
 - OS: AntiX 17
 - Browser: Firefox
 - Version 52.4.0

**Smartphone (please complete the following information):**
N/A

**Additional context**
This problem can be reproduced in web2py 2.16.1 and 2.17.1



Discussion:

Resolving patch:
diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py
--- a/gluon/sqlhtml.py
+++ b/gluon/sqlhtml.py
@@ -2693,13 +2693,13 @@ def linsert(lst, i, x):
                             dbset = dbset(SQLFORM.build_query(
                                 sfields, keywords))
                         rows = dbset.select(left=left, orderby=orderby,
-                                            cacheable=True, *selectable_columns)
+                                            cacheable=True, *expcolumns)
                     except Exception as e:
                         response.flash = T('Internal Error')
                         rows = []
                 else:
                     rows = dbset.select(left=left, orderby=orderby,
-                                        cacheable=True, *selectable_columns)
+                                        cacheable=True, *expcolumns)
 
                 value = exportManager[export_type]
                 clazz = value[0] if hasattr(value, '__getitem__') else value


Test patch: