Link to home
Start Free TrialLog in
Avatar of BrianMc1958
BrianMc1958

asked on

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

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?

Thanks,
BrianMc1958

Avatar of BrianMc1958
BrianMc1958

ASKER

BTW, I'm in VS2005.
ASKER CERTIFIED SOLUTION
Avatar of topdog770
topdog770
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, I did try adding it here within Form1.Designer, near the bottom:

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

// BEGIN MY CODE
            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
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.fTG_Total_2DataSet)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.mlaKYCParmBindingSource)).EndInit();
            this.ResumeLayout(false);


However, it still says there are zero rows.

(BTW, I will accept an answer of "Read The !@#$%^ Manual" here if necessary...)
Above code is in InitializeComponent, in the "#region Windows Form Designer generated code".
I have figured how to move my code to:

dataGridView1_DataBindingComplete

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?