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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Well, I did try adding it here within Form1.Designer, near the bottom:
this.Load += new System.EventHandler(this.F orm1_Load) ;
// BEGIN MY CODE
for (int i = 0; i < dataGridView1.Columns.Coun t; i++)
{
dataGridView1.Columns[i].V isible = false;
}
System.Console.Out.WriteLi ne("Rows:" + this.dataGridView1.Rows.Co unt);
for (int i = 0; i < this.dataGridView1.Rows.Co unt; i++)
{
System.Console.Out.WriteLi ne("i" + i);
for (int j = 0; j < this.dataGridView1.Columns .Count; j++)
{
System.Console.Out.WriteLi ne("j" + j + " val:" + this.dataGridView1[i, j]);
if (this.dataGridView1[i, j] != null)
dataGridView1.Columns[j].V isible = true;
}
}
// END MY CODE
((System.ComponentModel.IS upportInit ialize)(th is.dataGri dView1)).E ndInit();
((System.ComponentModel.IS upportInit ialize)(th is.fTG_Tot al_2DataSe t)).EndIni t();
((System.ComponentModel.IS upportInit ialize)(th is.mlaKYCP armBinding Source)).E ndInit();
this.ResumeLayout(false);
However, it still says there are zero rows.
(BTW, I will accept an answer of "Read The !@#$%^ Manual" here if necessary...)
this.Load += new System.EventHandler(this.F
// BEGIN MY CODE
for (int i = 0; i < dataGridView1.Columns.Coun
{
dataGridView1.Columns[i].V
}
System.Console.Out.WriteLi
for (int i = 0; i < this.dataGridView1.Rows.Co
{
System.Console.Out.WriteLi
for (int j = 0; j < this.dataGridView1.Columns
{
System.Console.Out.WriteLi
if (this.dataGridView1[i, j] != null)
dataGridView1.Columns[j].V
}
}
// END MY CODE
((System.ComponentModel.IS
((System.ComponentModel.IS
((System.ComponentModel.IS
this.ResumeLayout(false);
However, it still says there are zero rows.
(BTW, I will accept an answer of "Read The !@#$%^ Manual" here if necessary...)
ASKER
Above code is in InitializeComponent, in the "#region Windows Form Designer generated code".
ASKER
I have figured how to move my code to:
dataGridView1_DataBindingC omplete
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.WriteLi ne("j" + j + " val:" + this.dataGridView1[i, j]);
Is that syntax right ([i,j]) for specifying "row,col" in a DataGrid View?
dataGridView1_DataBindingC
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.WriteLi
Is that syntax right ([i,j]) for specifying "row,col" in a DataGrid View?
ASKER