By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,876 Members | 1,239 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,876 IT Pros & Developers. It's quick & easy.

DataGridView properties not being set when called from Form Load

P: 3
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:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12.     public partial class formOrderReview : Form
  13.     {
  14.         //public formOrderReview()
  15.         //{
  16.         //    InitializeComponent();
  17.         //}
  18.         DataGridView dataGridView1 = new DataGridView();
  19.         Button button1 = new Button();
  20.  
  21.         public formOrderReview()
  22.             : base()
  23.         {
  24.             Controls.Add(dataGridView1);
  25.             Controls.Add(button1);
  26.             button1.Click += new EventHandler(button1_Click);
  27.             button1.Top = 175;
  28.             button1.Left = 300;
  29.             button1.Text = "Reset";
  30.             dataGridView1.Width = 450;
  31.  
  32.             this.Width = 750;
  33.             this.Height = 400;
  34.  
  35.             DisplayOrderDetails();
  36.         }
  37.  
  38.         private void DisplayOrderDetails()
  39.         {
  40.             dataGridView1.DataSource = GetOrderData("");
  41.  
  42.             foreach (DataGridViewRow row in dataGridView1.Rows)
  43.             {
  44.                 if ((row.Cells["IsDeleted"].Value) != null && (row.Cells["IsDeleted"].Value) != DBNull.Value)
  45.                 {
  46.                     if (((bool)row.Cells["IsDeleted"].Value) == true)
  47.                     {
  48.                         row.Cells[dataGridView1.ColumnCount - 1].Value = 1;
  49.                         row.Cells[dataGridView1.ColumnCount - 1].ReadOnly = true;
  50.                         row.Cells[dataGridView1.ColumnCount - 1].Style.BackColor = Color.LightGray;
  51.                     }
  52.                 }
  53.             }
  54.          }
  55.  
  56.         public DataTable GetOrderData(string dummy)
  57.         {
  58.  
  59.             DataTable dt = new DataTable();
  60.             dt.Columns.Add("OrderTicketID", typeof(int));
  61.             dt.Columns.Add("Description", typeof(string));
  62.             dt.Columns.Add("Section", typeof(string));
  63.             dt.Columns.Add("Row", typeof(string));
  64.             dt.Columns.Add("Seat", typeof(string));
  65.             dt.Columns.Add("IsDeleted", typeof(bool));
  66.  
  67.             for (int i = 0; i < 10; i++)
  68.             {
  69.                 dt.Rows.Add(i, "Description " + i, "Section " + i, "Row " + i, "Seat " + i, i);
  70.             }
  71.             return dt;
  72.         }
  73.  
  74.         [STAThreadAttribute()]
  75.         public static void Main()
  76.         {
  77.             Application.Run(new formOrderReview());
  78.         }
  79.  
  80.         private void button1_Click(object sender, EventArgs e)
  81.         {
  82.              DisplayOrderDetails();
  83.         }
  84.     }
  85. }
Dec 15 '09 #1
Share this Question
Share on Google+
6 Replies


tlhintoq
Expert 2.5K+
P: 3,525
why doesn't it do it the first time when the function is called as the form loads?
Is it just me missing it, or do you not have a method for the Form_Load event?
If you are expecting something to happen at Form.Load then you should make a method handling that specifically.

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
I know it seems like the object exists, but in some ways it might not at time of construction. Try calling the same methods when the form loads.
Dec 15 '09 #2

P: 3
Hi - I already created the form with a Form load event and it didn't work, so I tried it this way instead. It's the same result either way. I have another form that does the same thing - loads a grid from the Form load event (which doesn't work) and reloads from a button click event (which works). Been going on for over a week now, starting to crack me up!
Dec 16 '09 #3

P: 1
I had an identical problem (I'd set the row background color but it wouldn't work until the fade out and fade in again of the form had happened once, that containded the datagridview) and found that it was remedied by ensuring the data grid view was visible when setting the row background color.
I'm using VS2008 too for a winforms project. .not sure if it matters, but. . .
Apr 23 '10 #4

P: 3
I never quite figured it out but it seems to be when the grid is populated in the form load event the problem happens, so I populated the grid from the form shown event instead which works. It's not ideal as it takes a few seconds to do the binding and to display it properly but it's the best I could come up with.
Apr 24 '10 #5

Plater
Expert 5K+
P: 7,872
The colors do not persist. They get reset everytime there is a refresh/redraw of the control.
You need to deal with the CellFormating event if you want cell style changes to persist.
Apr 26 '10 #6

P: 1
You Can do it on load of page then it will work.
Like add this Code.
Expand|Select|Wrap|Line Numbers
  1. private void Pagename_Load(object sender, EventArgs e)
  2.         {
  3.            DataTable objDT = null;
  4.             DataTable();
  5.             BindDataTable();
  6.         }
also Assign this function on load event of window form.
Jul 17 '14 #7

Post your reply

Sign in to post your reply or Sign up for a free account.