DataGridView properties not being set when called from Form Load

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ciaran
    New Member
    • Dec 2009
    • 3

    DataGridView properties not being set when called from Form Load

    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:

    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();
            }
        }
    }
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    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.

    Comment

    • Ciaran
      New Member
      • Dec 2009
      • 3

      #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!

      Comment

      • kek01
        New Member
        • Apr 2010
        • 1

        #4
        Make sure the datagridview is visible

        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. . .
        Last edited by kek01; Apr 23 '10, 09:56 PM. Reason: Additional info added

        Comment

        • Ciaran
          New Member
          • Dec 2009
          • 3

          #5
          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.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            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.

            Comment

            • Devendra79
              New Member
              • Jul 2014
              • 1

              #7
              You Can do it on load of page then it will work.
              Like add this Code.
              Code:
              private void Pagename_Load(object sender, EventArgs e)
                      {
                         DataTable objDT = null;
                          DataTable();
                          BindDataTable();
                      }
              also Assign this function on load event of window form.
              Last edited by Rabbit; Jul 17 '14, 04:01 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

              Comment

              Working...