Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

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

0
BrianMc1958
Asked:
BrianMc1958
  • 4
1 Solution
 
BrianMc1958Author Commented:
BTW, I'm in VS2005.
0
 
topdog770Commented:
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..
0
 
BrianMc1958Author Commented:
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...)
0
 
BrianMc1958Author Commented:
Above code is in InitializeComponent, in the "#region Windows Form Designer generated code".
0
 
BrianMc1958Author Commented:
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?  
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now