簡體   English   中英

單元格值將不會更改DataGridview

[英]Cell Value Will Not Change DataGridview

我正在使用EF6 / MySQL填充DataGridView。 我需要將列(0或1)的值轉換為相應的值(“藍色”或“綠色”),然后更改其樣式。 以下代碼執行無錯誤,但是單元格未更改。

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

我認為DataGridViewCellFormatting(...)事件可能更有效,因為那是該事件的目的。 嘗試這樣的事情:

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

希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM