简体   繁体   中英

WPF DataGrid. Change single cell background

I have Data Table with around 66 columns and 4000 rows

each Row comes to some category of some conditionally based coloring

I am very new to WPF actually i have implemented some condition based coloring the rows in datagridview but WPF as component DATA GRID

How to achieve cell based coloring based on cell value ? in WPF

earlier i was doing some thing like this in Win forms

public DataGridView colorGridview(DataGridView G)
        {
            string[] cellsrequired = {"Colnames1",""colname2};

            foreach (DataGridViewRow item in G.Rows)
            {

                foreach (DataGridViewCell cell in item.Cells)
                {
                    if (cellsrequired.Contains(cell.OwningColumn.HeaderText))
                    {
                        string str = cell.FormattedValue.ToString().Trim();
                        //  n / a
                        if (str != "N/A")// Or your condition 
                        {

                            if (str == "SKIP")
                            {
                                cell.Style.BackColor = Color.Orange;
                            }

                            else if (str == "FAIL")
                            {
                                cell.Style.BackColor = Color.Red;
                            }


                            else if (str == "INC")
                            {
                                cell.Style.BackColor = Color.Yellow;
                            }


                            else
                            {
                                cell.Style.BackColor = Color.SpringGreen;
                            }


                        }




                    }
                }

            }


            foreach (DataGridViewRow item in G.Rows)
            {
                if (object.Equals(item.Cells[35].Value, "FAIL"))
                {
                    var myparts = item.Cells[0].Value.ToString();

                    String[] CondtionsonCA = Getcondtion(myparts);

                    foreach (DataGridViewCell cell2 in item.Cells)
                    {
                        if (CondtionsonCA.Contains(cell2.OwningColumn.HeaderText))
                        {

                            string str = cell2.FormattedValue.ToString().Trim();
                            cell2.Style.BackColor = checkForColour(str);

                        }
                    }
                }
            }

            return G;
        }

You could use a value converter that implements the IValueConverter interface. The article I've linked to does a good job of describing the process but basically it's a small class that takes any input value, inspects it, and then returns what ever you want. For example, if the cell value = Good, return the color green, if the cell value = bad return the color red. You then need to bind the cell style property (eg background) to your cell value and set the converter as a parameter for that binding.

background = "{Binding ElementName=txtValue, Path=Text, Converter={StaticResource CellValueToColorConverter}}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM