简体   繁体   English

单元格值将不会更改DataGridview

[英]Cell Value Will Not Change DataGridview

I am using EF6/MySQL to populate a DataGridView. 我正在使用EF6 / MySQL填充DataGridView。 I need to convert the value of a Column (0 or 1) to a corresponding Value ("Blue" or "Green"), and change it's Style. 我需要将列(0或1)的值转换为相应的值(“蓝色”或“绿色”),然后更改其样式。 The following code executes without error, however the Cells do NOT change. 以下代码执行无错误,但是单元格未更改。

 using (var db = new hyperion_paymenttrackerEntities())
            {
                dgvTracker.Rows.Clear();
               // Goal = Convert.ToDecimal(db.configs.Where(o => o.Name == "goal").Select(o => o.Value).FirstOrDefault());
                var data = from c in db.payments.AsEnumerable()
                    select new
                    {
                        c.CollectorName,
                        c.Lead,
                        c.C2ndTalkOff,
                        c.PaymentType,
                        c.PaymentAmount
                    };
                dgvTracker.DataSource = data.ToList();
            }
            var dataGridViewColumn = dgvTracker.Columns["CollectorName"];
            if (dataGridViewColumn != null)
                dataGridViewColumn.HeaderText = "Collector";
            var gridViewColumn = dgvTracker.Columns["Lead"];
            if (gridViewColumn != null) gridViewColumn.HeaderText = "Generated By";
            var viewColumn = dgvTracker.Columns["C2ndTalkOff"];
            if (viewColumn != null) viewColumn.HeaderText = "Assisted By";
            var column = dgvTracker.Columns["PaymentType"];
            if (column != null) column.HeaderText = "Type";
            var dataGridViewColumn1 = dgvTracker.Columns["PaymentAmount"];
            if (dataGridViewColumn1 != null)
                dataGridViewColumn1.HeaderText = "Payment Amount";


            //blue = 0 green = 1
            foreach (DataGridViewRow row in dgvTracker.Rows)
            {
                var cell = row.Cells["PaymentType"];
                if ((string) cell.Value == "0")
                {
                    cell.Style = BlueStyle();
                    cell.Value = "BLUE";
                }
                else
                {
                    cell.Style = GreenStyle();
                    cell.Value = "GREEN";
                }
            }

I think the CellFormatting(...) event of the DataGridView may be more efficient since that is what the event is for. 我认为DataGridViewCellFormatting(...)事件可能更有效,因为那是该事件的目的。 Try something like this: 尝试这样的事情:

    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)    
    {
        if (e.ColumnIndex == 1 )   // Change int value of '1' to the Column you want to edit
        {
            if (!ReferenceEquals(e.Value, DBNull.Value))    // You don't need this check if you are not doing any conversion of the e.Value to a numeric value.
            {
                if (e.Value.ToString() == "0") // Change colour and value to blue.  
                {
                   // Use this lines to change the color of just the cell
                    DataGridViewCellStyle style = new DataGridViewCellStyle();
                    style.BackColor = Color.Blue;
                    style.ForeColor = Color.White;
                    style.Font = new System.Drawing.Font("Arial", 10, FontStyle.Regular);
                    e.CellStyle = style;
                    e.Value = "BLUE";                     
                }
                if (e.Value.ToString() == "1")  // Change colour and value to green
                {
                    // Use this lines to change the color of just the cell
                    DataGridViewCellStyle style = new DataGridViewCellStyle();
                    style.BackColor = Color.Green;
                    style.ForeColor = Color.White;
                    style.Font = new System.Drawing.Font("Arial", 10, FontStyle.Regular);
                    e.CellStyle = style;
                    e.Value = "GREEN";
                }
            }
        }
    }

I hope it helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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