how to set row border color depandent on row's cell value?

Hi~
May I set row border color when row's cell value match condition?


8 Replies 1 reply marked as answer

TP Tamilarasan Paranthaman Syncfusion Team May 10, 2022 12:43 PM UTC

Hi Jimmy,


Currently, DataGrid does not provide direct support to customising the border for any cell based on condition. But, you can achieve your requirement at sample level by following these steps,


Step 1: Set the gridLinesVisibility to GridLinesVisibility.none


Step 2: In the DataGridSource.buildRow method, you can set the border for the individual cell with the required color based on the conditions.


class EmployeeDataSource extends DataGridSource {

  EmployeeDataSource(List<Employee> employees) {

    buildDataGridRow(employees);

    getRowIndex();

  }

 

  void buildDataGridRow(List<Employee> employeeData) {

    dataGridRow = employeeData.map<DataGridRow>((employee) {

      return DataGridRow(cells: [

        DataGridCell<int>(columnName: 'id', value: employee.id),

        DataGridCell<String>(columnName: 'name', value: employee.name),

        DataGridCell<String>(

            columnName: 'designation', value: employee.designation),

        DataGridCell<int>(columnName: 'salary', value: employee.salary),

      ]);

    }).toList();

  }

 

  List<DataGridRow> dataGridRow = <DataGridRow>[];

 

  @override

  List<DataGridRow> get rows => dataGridRow.isEmpty ? [] : dataGridRow;

 

  @override

  DataGridRowAdapter? buildRow(DataGridRow row) {

    return DataGridRowAdapter(

        cells: row.getCells().map<Widget>((dataGridCell) {

      return Container(

          decoration: BoxDecoration(

              border: Border(

            bottom: BorderSide(width: 1.0, color: getColor(dataGridCell, row)),

            right: BorderSide(width: 1.0, color: getColor(dataGridCell, row)),

            top: BorderSide(width: 1.0, color: getColor(dataGridCell, row)),

          )),

          alignment: Alignment.center,

          padding: const EdgeInsets.symmetric(horizontal: 16.0),

          child: Text(

            dataGridCell.value.toString(),

            overflow: TextOverflow.ellipsis,

          ));

    }).toList());

  }

 

  List<int> rowIndex = [];

 

  getRowIndex() {

    for (DataGridRow row in dataGridRow) {

      int index = dataGridRow.indexOf(row);

      for (int i = 0; i <= 3; i++) {

        if (row.getCells()[i].value == 33000) {

          rowIndex.add(index);

        }

      }

    }

  }

 

  Color getColor(DataGridCell dataGridCell, DataGridRow row) {

    int index = dataGridRow.indexOf(row);

 

    if (rowIndex.contains(index)) {

      return Colors.red;

    } else {

      return Colors.grey.withOpacity(0.45);

    }

  }

}


 


We have also prepared a sample for that. Please check this sample and let us know if you need any further assistance.


Sample Link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/sample1207324536              


Regards,

Tamilarasan



JH Jimmy Huang May 13, 2022 08:57 AM UTC

Hi Tamilarasan

Thanks for your sample.

I find currentCellStyle property of SfDataGridThemeData in SfDataGridTheme:
https://help.syncfusion.com/flutter/datagrid/selection#current-cell

Do you have plan add  currentRowStyle on later datagrid version?





TP Tamilarasan Paranthaman Syncfusion Team May 16, 2022 01:46 PM UTC

Hi Jimmy,


We are a little bit unclear about your requirement. Can you please confirm whether you need to change the current cell’s row background color? It would be helpful if you provided more details about your requirement.


Regards,

Tamilarasan



JH Jimmy Huang May 17, 2022 09:15 AM UTC

Hi Tamilarasan ~
When set
navigationMode to GridNavigationMode.cell can move cell by press tab then cell have highlight border color.
In my case when set navigationMode to GridNavigationMode.row I want to press keyboard(bottom and top arrow) to move row and
highlight row border color.

And there are not like moveCurrentCellTo to use , may be can also add moveCurrentRowTo.





TP Tamilarasan Paranthaman Syncfusion Team May 18, 2022 10:49 AM UTC

Hi Jimmy,


As per your requirement, if you set the navigation mode to GridNavigationMode.row then the current cell style color which is set in the DataGridCurrentCellStyle.borderColor is applied to the current row border color too. We have prepared the sample for that. Please check the sample and let us know if it meets your requirement.


Code Snippet:


  @override

  Widget build(BuildContext context) {

    return Scaffold(

        appBar: AppBar(title: const Text('Flutter SfDataGrid')),

        body: SfDataGridTheme(

            data: SfDataGridThemeData(

                currentCellStyle: const DataGridCurrentCellStyle(

                    borderWidth: 1, borderColor: Colors.red)),

            child: SfDataGrid(

              source: _employeeDataSource,

              columns: getColumns,

              columnWidthMode: ColumnWidthMode.fill,

              navigationMode: GridNavigationMode.row,

              selectionMode: SelectionMode.multiple,

            )));

  }

}



Sample Link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/sample-168781058


Regards,

Tamilarasan


Marked as answer

JH Jimmy Huang July 14, 2022 08:57 AM UTC

Hi Tamilarasan ~
your reply is useful,
may be can add to doc,
thanks very much : >



TP Tamilarasan Paranthaman Syncfusion Team July 15, 2022 10:13 AM UTC

Hi Jimmy,


Thanks for the update.


We are glad that the provided response meets your requirement. We have considered your request as a UG documentation update. The updated content will be published soon. We will let you know once it is published. We appreciate your patience and understanding till then.


Regards,

Tamilarasan



TP Tamilarasan Paranthaman Syncfusion Team September 30, 2022 11:57 AM UTC

Hi Jimmy,


We have updated and published the UG document as per the request. Please find the updated documentation link below.


UG Documentation: https://help.syncfusion.com/flutter/datagrid/selection#current-cell


Regards,

Tamilarasan


Loader.
Up arrow icon