473,413 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

DataGridView properties not being set when called from Form Load

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
6 10541
tlhintoq
3,525 Expert 2GB
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
Ciaran
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
kek01
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
Ciaran
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
7,872 Expert 4TB
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
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

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

Similar topics

9
by: helpful sql | last post by:
Hi all, I have a DataGridView control in my Windows Forms application. I am binding it to a DataTable in another assembly. The name of the assembly is ActivityListDALC. I want to bind my grid to...
2
by: Steve | last post by:
Hi- OK, I've got a DataGridView, I've created a BindingSource from one of my Business Entity object (based on generated classes from EntitySpaces) I've left the default column setup so that all...
0
by: AFB | last post by:
Hi: I have a form called "Form1" and another called "Form2". I have a panel in Form1 and I add to the panel the Form2. The code is something like... Dim FormuGestorProyectos As From2...
4
by: Martin Arvidsson | last post by:
Hi! I have used the SqlCommand and DataReader to read data to a DataSet This dataset is the used to set the properties; DataMember and DataSource of the DataGridView. Now what i want to do is...
3
by: Johnny E. Jensen | last post by:
Hello Dot sure if this it the right group but here goes. I'am using the DataGridView multiple times in my application, and then i'll read a book on inherience, and that opend a new world for...
3
by: =?Utf-8?B?Sm9obiBCdW5keQ==?= | last post by:
New to databinding in vs2005, I always did it manually in 2003. I have no problem loading comboboxes, and a change in that combobox changes the data in the textboxes but I can not figure out a way...
7
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is...
3
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm trying to add a datagridview control to a Windows Form to display read-only information in visual basic 2005. My understanding is that datareader will be faster for this purpose. I have the...
1
by: mtembene | last post by:
I have a windows form "BaseForm" that contains a DataGridView that is not bound to any datasources and a button. Both of these controls have a modifier of "Protected Internal" and none of the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.