值没有根据条件c#

时间:2017-01-16 20:23:21

标签: c# datagridviewcolumn minmax

如果cell.value是单元格必须包含的所有值的最小值,我想根据条件更改datagridview列的值" L"那很低。如果该值是所有cell.value =" H"中的最高值。休息应该是中等的。这就是我到目前为止所做的:

 //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");
                }


            }
        } 
    }

这就是结果:

enter image description here

我的问题是中等和高值显示但有时它会逃脱最低值并且不显示任何&#34; L&#34;在某些专栏。生成的数据是随机数据。

1 个答案:

答案 0 :(得分:1)

我清理了你的代码,我没有看到你的错误。如果这有助于您解决问题,请告诉我。

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";
                }
            }
        }
    }
}