SfDataGrid doesn't take up all available space

I have used an SfDataGrid and everything works fine except when I attempt to make it use all the available space it fails. The columns on the right aren't visible even though the sfDataGrid has space for it.Any help would be appreciated

Image_4549_1716373128457

Thank you


9 Replies 1 reply marked as answer

AP Abinesh Palanisamy Syncfusion Team May 23, 2024 05:56 AM UTC

Hi Ammar,


Based on the provided image, we can't address your issue clearly. It seems that you are using some other widgets as the parent to the DataGrid. To assist you further and better understand the issue, we kindly request you to provide additional details or steps for placing the SfDataGrid in your sample or scenarios that may help us reproduce the problem. This additional information will enable us to thoroughly address your request and provide an appropriate solution.


Regards,

Abinesh P



AM Ammar May 23, 2024 07:09 AM UTC

Hi Abinesh,

This is the code I have written I hope it helps to solve my issue

import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
import 'dart:convert';

class ListDialogWidget extends StatefulWidget {
  const ListDialogWidget({
    super.key,
  });

  @override
  State<ListDialogWidget> createState() => _ListDialogWidgetState();
}

class _ListDialogWidgetState extends State<ListDialogWidget> {
  List<Map<String, dynamic>> _rows = [];

  @override
  void initState() {
    _loadRowData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      child: Container(
        padding: const EdgeInsets.all(8.0),
        width: double.infinity,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(16.0),
          color: Theme.of(context).scaffoldBackgroundColor,
        ),
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                TextButton.icon(
                  label: const Text("Filter"),
                  onPressed: () {},
                  icon: const Icon(Icons.sort),
                ),
              ],
            ),
            const Divider(
              color: Colors.black54,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                SfDataGrid(
                  source: _JsonDataSource(
                      columnNames: _getColumns(), jsonData: _rows),
                  columns: _getColumns().map<GridColumn>((String columnName) {
                    return GridColumn(
                        columnName: columnName,
                        label: Text(columnName.toUpperCase()));
                  }).toList(),
                  allowFiltering: true,
                  allowColumnsResizing: true,
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Future<void> _loadRowData() async {
    // Sample JSON data
    String jsonString = '''
   
      [
        {"id": "123", "salary":200, "testcolumn":"testvalue", "testcolumn2":"testvalue2"},
        {"id": "25", "salary":200, "testcolumn":"testvalue", "testcolumn2":"testvalue2"},
        {"id": "27", "salary":200, "testcolumn":"testvalue", "testcolumn2":"testvalue2"},
        {"id": "612", "salary":200, "testcolumn":"testvalue", "testcolumn2":"testvalue2"}
       
      ]
    ''';
    List<Map<String, dynamic>> jsonData =
        json.decode(jsonString).cast<Map<String, dynamic>>();

    setState(() {
      _rows = jsonData;
    });
  }

  List<String> _getColumns() {
    if (_rows.isNotEmpty) {
      // Extract column names from the first row of JSON data
      Map<String, dynamic> firstRow = _rows.first;
      return firstRow.keys.toList();
    }
    return [];
  }
}

class _JsonDataSource extends DataGridSource {
  final dynamic _data;
  final List<String> columnNames;
  _JsonDataSource({required this.columnNames, required dynamic jsonData})
      : _data = jsonData;

  @override
  List<DataGridRow> get rows => generateRows();

  List<DataGridRow> generateRows() {
    return _data.map<DataGridRow>((data) {
      return DataGridRow(
          cells: columnNames.map<DataGridCell>((colName) {
        return DataGridCell<dynamic>(columnName: colName, value: data[colName]);
      }).toList());
    }).toList();
  }

  @override
  DataGridRowAdapter? buildRow(DataGridRow row) {
    return DataGridRowAdapter(
        cells: row.getCells().map<Widget>((dataGridCell) {
      return Container(
          alignment: (dataGridCell.columnName == 'id' ||
                  dataGridCell.columnName == 'salary')
              ? Alignment.centerRight
              : Alignment.centerLeft,
          padding: const EdgeInsets.symmetric(horizontal: 16.0),
          child: GestureDetector(
              child: Text(
            dataGridCell.value.toString(),
            style: const TextStyle(
                fontWeight: FontWeight.bold,
                decoration: TextDecoration.underline,
                color: Colors.blue),
            overflow: TextOverflow.ellipsis,
          )));
    }).toList());
  }
}



AP Abinesh Palanisamy Syncfusion Team May 23, 2024 11:02 AM UTC

Hi Ammar,


The SfDataGrid takes the width and height of its parent widget, occupying the space based on the parent widget's dimensions. In your case, to ensure that the SfDataGrid takes up all available space and that the columns are visible, you need to use the Expanded widget. The Expanded widget makes its child widget expand to fill the available space within a Row or Column.


Wrapping the Row that contains the SfDataGrid in an Expanded widget ensures that this Row takes up all available space in the parent Column. Similarly, wrapping the SfDataGrid itself in an Expanded widget ensures that the DataGrid takes up all available space within the Row.


In your case, you are using the SfDataGrid as the only child in the Row widget. Therefore, instead of using a Row, you can directly wrap the SfDataGrid with an Expanded widget as the parent, without the need for a Row widget. If you need to place multiple children in the Row widget, you can use two Expanded widgets: one as the parent of the Row and another as the parent of the SfDataGrid. By using the Expanded widget, the SfDataGrid will take up all the available space of the Row,  making the mainAxisAlignment property ineffective. This approach ensures that all columns are visible and properly laid out.


We have attached a sample for your reference. Please consult the following sample for further information. If you have any further concerns, please feel free to reach out.


Regards,

Abinesh P


Attachment: SfDataGrid_4a2083a7.zip


AM Ammar May 23, 2024 12:56 PM UTC

Thank you abinesh this helps solve my problem for the most part but right side border is missing

Image_2363_1716468965196



Here's my updated code

import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
import 'dart:convert';

class ListDialogWidget extends StatefulWidget {
  const ListDialogWidget({
    super.key,
  });

  @override
  State<ListDialogWidget> createState() => _ListDialogWidgetState();
}

class _ListDialogWidgetState extends State<ListDialogWidget> {
  List<Map<String, dynamic>> _rows = [];

  @override
  void initState() {
    _loadRowData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      child: Container(
        padding: const EdgeInsets.all(8.0),
        width: double.infinity,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(16.0),
          color: Theme.of(context).scaffoldBackgroundColor,
        ),
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                TextButton.icon(
                  label: const Text("Filter"),
                  onPressed: () {},
                  icon: const Icon(Icons.sort),
                ),
              ],
            ),
            const Divider(
              color: Colors.black54,
            ),
            Expanded(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Expanded(
                    child: SfDataGrid(
                      columnWidthMode: ColumnWidthMode.fill,
                      source: _JsonDataSource(
                          columnNames: _getColumns(), jsonData: _rows),
                      columns:
                          _getColumns().map<GridColumn>((String columnName) {
                        return GridColumn(
                            columnName: columnName,
                            label: Text(columnName.toUpperCase()));
                      }).toList(),
                      allowFiltering: true,
                      allowColumnsResizing: true,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Future<void> _loadRowData() async {
    // Sample JSON data
    String jsonString = '''
   
      [
        {"id": "Xyz", "salary":200, "testcolumn":"testvalue", "testcolumn2":"testvalue2"},
        {"id": "y2", "salary":200, "testcolumn":"testvalue", "testcolumn2":"testvalue2"},
        {"id": "X@%", "salary":200, "testcolumn":"testvalue", "testcolumn2":"testvalue2"},
        {"id": "5612", "salary":200, "testcolumn":"testvalue", "testcolumn2":"testvalue2"}
       
      ]
    ''';
    List<Map<String, dynamic>> jsonData =
        json.decode(jsonString).cast<Map<String, dynamic>>();

    setState(() {
      _rows = jsonData;
    });
  }

  List<String> _getColumns() {
    if (_rows.isNotEmpty) {
      // Extract column names from the first row of JSON data
      Map<String, dynamic> firstRow = _rows.first;
      return firstRow.keys.toList();
    }
    return [];
  }
}

class _JsonDataSource extends DataGridSource {
  final dynamic _data;
  final List<String> columnNames;
  _JsonDataSource({required this.columnNames, required dynamic jsonData})
      : _data = jsonData;

  @override
  List<DataGridRow> get rows => generateRows();

  List<DataGridRow> generateRows() {
    return _data.map<DataGridRow>((data) {
      return DataGridRow(
          cells: columnNames.map<DataGridCell>((colName) {
        return DataGridCell<dynamic>(columnName: colName, value: data[colName]);
      }).toList());
    }).toList();
  }

  @override
  DataGridRowAdapter? buildRow(DataGridRow row) {
    return DataGridRowAdapter(
        cells: row.getCells().map<Widget>((dataGridCell) {
      return Container(
          // alignment: Alignment.center,
          padding: const EdgeInsets.symmetric(horizontal: 16.0),
          child: GestureDetector(
              child: Text(
            dataGridCell.value.toString(),
            style: const TextStyle(
                fontWeight: FontWeight.bold,
                decoration: TextDecoration.underline,
                color: Colors.blue),
            overflow: TextOverflow.ellipsis,
          )));
    }).toList());
  }
}



AP Abinesh Palanisamy Syncfusion Team May 24, 2024 06:40 AM UTC

Hi Ammar,


As of now, we have already identified this as a bug and logged a report in our feedback portal. We will fix the reported issue and include the changes in our upcoming weekly patch release, which is expected to be rolled out on June 4, 2024. We will notify you once it is released and appreciate your patience until then. You can also track the status of the bug using the feedback link below.


Feedback link : 57918


Regards,

Abinesh P



AM Ammar May 24, 2024 08:03 AM UTC

That's unfortunate, but I understand and thank you so much for all your assistance. I'll wait for the patch

Please notify me when it is available



AP Abinesh Palanisamy Syncfusion Team June 4, 2024 12:57 PM UTC

Hi Ammar,


We are glad to inform you that the reported problem has been resolved on our end. Therefore, kindly update SfDataGrid to the latest version(25.2.7). If you have any further queries, please don't hesitate to reach out. We are more than happy to assist you.


Regards,

Abinesh P




Marked as answer

AM Ammar June 5, 2024 06:52 AM UTC

Thank you Abinesh the issue has been fixed



AP Abinesh Palanisamy Syncfusion Team June 5, 2024 11:58 AM UTC

Hi Ammar ,

Glad that your issue is resolved!! We are closing this ticket now. Please reopen this ticket or open new ticket if you need any assistance. We are more than happy to assist you.


Regards,

Abinesh P


Loader.
Up arrow icon