I'm having a really strange issue with the DataGridView control in a VS2008 / .NET 3.5 winforms project. I have a simple form with a grid. In the form constructor I call a function to bind the grind to a DataTable, and then loop through the rows setting the background colour of the last cell to LightGrey and the cell itself to read-only if the column value is true. After the form finishes loading the code didn't work i.e. the cells are not set to LightGrey and are not read-only (even though when I step through the code I can see the properties being set). I then call the function again from a button, but this time the colour is changed to LightGrey and the cell is made read-only i.e. the code works.
I really don't understand this - why doesn't it do it the first time when the function is called as the form loads? Does the grid repaint itself somehow and reset any values that I set when the form finishes loading? Here's the form code:
I really don't understand this - why doesn't it do it the first time when the function is called as the form loads? Does the grid repaint itself somehow and reset any values that I set when the form finishes loading? Here's the form code:
Code:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class formOrderReview : Form { //public formOrderReview() //{ // InitializeComponent(); //} DataGridView dataGridView1 = new DataGridView(); Button button1 = new Button(); public formOrderReview() : base() { Controls.Add(dataGridView1); Controls.Add(button1); button1.Click += new EventHandler(button1_Click); button1.Top = 175; button1.Left = 300; button1.Text = "Reset"; dataGridView1.Width = 450; this.Width = 750; this.Height = 400; DisplayOrderDetails(); } private void DisplayOrderDetails() { dataGridView1.DataSource = GetOrderData(""); foreach (DataGridViewRow row in dataGridView1.Rows) { if ((row.Cells["IsDeleted"].Value) != null && (row.Cells["IsDeleted"].Value) != DBNull.Value) { if (((bool)row.Cells["IsDeleted"].Value) == true) { row.Cells[dataGridView1.ColumnCount - 1].Value = 1; row.Cells[dataGridView1.ColumnCount - 1].ReadOnly = true; row.Cells[dataGridView1.ColumnCount - 1].Style.BackColor = Color.LightGray; } } } } public DataTable GetOrderData(string dummy) { DataTable dt = new DataTable(); dt.Columns.Add("OrderTicketID", typeof(int)); dt.Columns.Add("Description", typeof(string)); dt.Columns.Add("Section", typeof(string)); dt.Columns.Add("Row", typeof(string)); dt.Columns.Add("Seat", typeof(string)); dt.Columns.Add("IsDeleted", typeof(bool)); for (int i = 0; i < 10; i++) { dt.Rows.Add(i, "Description " + i, "Section " + i, "Row " + i, "Seat " + i, i); } return dt; } [STAThreadAttribute()] public static void Main() { Application.Run(new formOrderReview()); } private void button1_Click(object sender, EventArgs e) { DisplayOrderDetails(); } } }
Comment