Search code examples
c#datagridviewcolumnminmax

values are not transforming in datagridview as per condition c#


I want to change the value of datagridview column according to the condition if the cell.value is minimum of the all values the cell must contain "L" , that is low. If the value is highest of all the cell.value = "H" rest should be medium. This is what i have done so far:

 //Function to display interaction as High, Medium and Low
    public  void ShowInteraction(DataGridView dv, string columnName)
    {
        //MAX 
        var Max = dv.Rows.Cast<DataGridViewRow>()
                    .Max(r => Convert.ToDouble(r.Cells[columnName].Value));





        //MIN 
        List<double> arr = new List<double>();

        foreach (DataGridViewRow row in dv.Rows)
        {

            if (row != null && row.Cells[columnName].Value != null)
            {

                arr.Add(Convert.ToDouble(row.Cells[columnName].Value.ToString()));
            }
        }

        double Min = arr.Min();

        //show interaction in datagridview as H, M nad L
        foreach (DataGridViewRow row in dv.Rows)
        {
            if (row.Cells[columnName].Value != null)
            {
                if ((double)row.Cells[columnName].Value ==  Min)
                {
                    row.Cells[columnName].Value = Convert.ToString( "L" );

                }
                else if ((double)row.Cells[columnName].Value == Max)
                {
                    row.Cells[columnName].Value = Convert.ToString( "H" );
                }
                else if ((double)row.Cells[columnName].Value < Max && (double)row.Cells[columnName].Value > Min)
                {
                    row.Cells[columnName].Value = Convert.ToString("M");
                }
                else if ((double)row.Cells[columnName].Value == 0.0000)
                {
                    row.Cells[columnName].Value = Convert.ToString("N");
                }

                else
                {
                    row.Cells[columnName].Value = Convert.ToString("L");
                }


            }
        } 
    }

And this the result:

enter image description here

My problem is that the medium and high values are showing but some times it escape the lowest value and do not show any "L" at some columns. The data generated is random data.


Solution

  • I cleaned up your code a little bit and I'm not seeing the error you are. Let me know if this helps you with your issue or not.

    public partial class MainForm : Form {
    
        public MainForm() {
            InitializeComponent();
            DataGridView dgv = new DataGridView();
            dgv.Columns.Add("columnName", "columnName");
            dgv.AllowUserToAddRows = false;
    
            dgv.Rows.Add(21.0);
            dgv.Rows.Add(15.0);
            dgv.Rows.Add(20.0);
            dgv.Rows.Add(25.0);
            dgv.Rows.Add(30.12354122);
    
            this.Controls.Add(dgv);
    
            ShowInteraction(dgv, "columnName");
        }
    
        public void ShowInteraction(DataGridView dv, string columnName) {
            List<double> values = dv.Rows.Cast<DataGridViewRow>().Select(row => Convert.ToDouble(row.Cells[columnName].Value)).ToList();
            double Max = values.Max();
            double Min = values.Min();
    
            foreach (DataGridViewRow row in dv.Rows)
            {
                if (row.Cells[columnName].Value != null)
                {
                    double cellValue = Convert.ToDouble(row.Cells[columnName].Value);
                    var cell = row.Cells[columnName];
                    if (cellValue == Min)
                    {
                        cell.Value = "L";
                    }
                    else if (cellValue == Max)
                    {
                        cell.Value = "H";
                    }
                    else if (cellValue < Max && cellValue > Min)
                    {
                        cell.Value = "M";
                    }
                    else if (cellValue == 0)
                    {
                        cell.Value = "N";
                    }
                    else
                    {
                        cell.Value = "L";
                    }
                }
            }
        }
    }