Reputation: 81
private void btnShowCausali_Click(object sender, EventArgs e)
{
DataGridView Dati = new DataGridView();
Dati.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
Dati.Location = new System.Drawing.Point(120, 40);
Dati.Name = "Dati";
Dati.RowTemplate.Height = 24;
Dati.Size = new System.Drawing.Size(979, 458);
Dati.TabIndex = 1;
Dati.Visible = true;
Dati.Columns.Add("id", "ID");
Dati.Columns.Add("causaliname", "Nome Causale");
Dati.Columns.Add("Identificationcode", "Codice Identificativo");
Dati.Columns.Add("expired", "Data di Scadenza");
grpDatore.Controls.Add(Dati);
}
When I run this code the DataGridView does not appear on the form. Just in case it could create problem, the button btnShowCausali
get created on the form load.
public void Form1_Load(object sender, EventArgs e)
{
Button btnShowCausali = new Button();
btnShowCausali.Text = "Causali";
btnShowCausali.Location = new Point(20, 20);
btnShowCausali.Size = new Size(120, 40);
}
By the way, I don't know why the button actually gets created but the DataGridView does not.
Upvotes: 0
Views: 1467
Reputation: 1301
First of all, if the button is created dynamically, add it to the UI.
Button btnShowCausali = new Button();
btnShowCausali.Text = "Causali";
btnShowCausali.Location = new Point(20, 20); // Make shure there aren't other controls in this point
btnShowCausali.Size = new Size(120, 40);
this.Controls.Add(btnShowCausali); //Adding the button to the form
Then, you have to attach an event handler for the Click
event of the button. In short, you have to tell to the button what to do when it is clicked. You have two options to add an event handler for the click event:
Code: add btnShowCausali.Click += btnShowCausali_Click;
after the InitializeComponent
function call (in the constructor) or in the Load
event of the form. Then add the function btnShowCausali_Click
:
private void btnShowCausali_Click(object sender, EventArgs e)
{
DataGridView Dati = new DataGridView();
Dati.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
Dati.Location = new System.Drawing.Point(120, 40);
Dati.Name = "Dati";
Dati.RowTemplate.Height = 24;
Dati.Size = new System.Drawing.Size(979, 458);
Dati.TabIndex = 1;
Dati.Visible = true;
Dati.Columns.Add("id", "ID");
Dati.Columns.Add("causaliname", "Nome Causale");
Dati.Columns.Add("Identificationcode", "Codice Identificativo");
Dati.Columns.Add("expired", "Data di Scadenza");
grpDatore.Controls.Add(Dati); // DataGridView added to grpDatore, not form. Make shure grpDatore is visible.
}
Designer: double click on the button. Visual Studio will do the magic for you.
Upvotes: 1
Reputation: 3161
Your event handler btnShowCausali_Click is not attached to the button in Form1_Load. Are you sure it is called? I also do not see adding this button to any container (Form, Panel, ...) Your DataGridView will be added to grpDatore control (not form) so have that in mind when you set Location.
Attach event:
btnShowCausali.Click += btnShowCausali_Click;
Upvotes: 2