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不会出现在窗体上。为了防止它产生问题,在表单加载上创建了按钮btnShowCausali
。
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);
}
顺便说一句,我不知道为什么按钮实际被创建但DataGridView没有。
答案 0 :(得分:2)
您的事件处理程序btnShowCausali_Click未附加到Form1_Load中的按钮。你确定它被称为? 我也没有看到将此按钮添加到任何容器(Form,Panel,...) 您的DataGridView将添加到grpDatore控件(而不是表单)中,因此在设置位置时请记住这一点。
附加活动:
btnShowCausali.Click += btnShowCausali_Click;
答案 1 :(得分:1)
首先,如果按钮是动态创建的,请将其添加到用户界面。
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
然后,您必须为按钮的Click
事件附加事件处理程序。简而言之,您必须告诉按钮单击时要执行的操作。您有两个选项可以为click事件添加事件处理程序:
代码:在btnShowCausali.Click += btnShowCausali_Click;
函数调用(在构造函数中)或表单的InitializeComponent
事件之后添加Load
。然后添加函数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.
}
设计师:双击按钮。 Visual Studio将为您带来魔力。