单元格值不会更改DataGridview

时间:2014-08-10 16:21:30

标签: c# mysql datagridview

我正在使用EF6 / MySQL来填充DataGridView。我需要将Column(0或1)的值转换为相应的Value(" Blue"或" Green"),然后更改它的Style。以下代码执行时没有错误,但单元格不会更改。

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

1 个答案:

答案 0 :(得分:0)

我认为 DataGridView CellFormatting(...)事件可能更有效,因为这就是事件的用途。尝试这样的事情:

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

我希望它有所帮助。