Breathtakingly dumb question: Where to put custom code in C# Windows app?

Posted on 2006-05-31
Last Modified: 2010-04-16
Dear Experts,

I'm trying to customize a drag-and-drop-created C# app for the first time.  The app has a DataGridView in "Form1.Designer".  Before the table is shown, I would like to make invisible all columns that have no data in them (all nulls).  Someone (anyoneis) has kindly suppied me with the code to do that.  However, I DON'T KNOW WHERE TO PUT IT!

If I include it in Form1.Designer, I find the data isn't actually loaded into the table yet, so no "rows" are found. (I also get a "Don't put anything here" message later...")  And I can't put it in "program.cs", because it's static.

I might mention that anyonis's code referenced the table directly, and I need to reference the DataGridView (I think), so maybe that is my mistake...

I know this may be the dumbest question in the history of Experts Exchange, but where do I put my own code in this case?


Question by:BrianMc1958

    Author Comment

    BTW, I'm in VS2005.
    LVL 12

    Accepted Solution

    At what point in your code is the datagrid populated?

    typically, it would be in response to an button press or the Form.Load event..

    Author Comment

    Well, I did try adding it here within Form1.Designer, near the bottom:

                this.Load += new System.EventHandler(this.Form1_Load);

                for (int i = 0; i < dataGridView1.Columns.Count; i++)
                    dataGridView1.Columns[i].Visible = false;

                System.Console.Out.WriteLine("Rows:" + this.dataGridView1.Rows.Count);
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                    System.Console.Out.WriteLine("i" + i);
                    for (int j = 0; j < this.dataGridView1.Columns.Count; j++)
                        System.Console.Out.WriteLine("j" + j + " val:" + this.dataGridView1[i, j]);
                        if (this.dataGridView1[i, j] != null)
                            dataGridView1.Columns[j].Visible = true;
    // END MY CODE

    However, it still says there are zero rows.

    (BTW, I will accept an answer of "Read The !@#$%^ Manual" here if necessary...)

    Author Comment

    Above code is in InitializeComponent, in the "#region Windows Form Designer generated code".

    Author Comment

    I have figured how to move my code to:


    and it now seems to be processing some rows, anyway.  However, I think my syntax is wrong for specifying "current cell".  It's giving my an invalid index message the first time it hits:

    System.Console.Out.WriteLine("j" + j + " val:" + this.dataGridView1[i, j]);

    Is that syntax right ([i,j]) for specifying "row,col" in a DataGrid View?  

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Hire Top Freelancers to Complete C# Projects

    Source the talented Expert Exchange community
    for top quality work on your C# projects.

    Hire the best. Collaborate easily. Get quality work.

    Suggested Solutions

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now