Reputation: 291
I have a datagridview with a clickevent assigned to it. This datagridview is on a tabpage. When the user clicks the datagridview, I want a second datagridview to be created below it. This is the code for the click event. The only line that is functioning properly is the Console.WriteLine("dataGridView1 clicked") line. Everything else is being ignored.
void dataGridView1_Click(object sender, EventArgs e)
{
DataGridView dataGridView1 = new DataGridView();
dataGridView1.ColumnCount = 6;
DataGridViewColumn column1 = new DataGridViewColumn();
column1 = dataGridView1.Columns[0];
dataGridView1.Columns[0].HeaderText = "column";
tabPage.Controls.Add(dataGridView1);
Console.WriteLine("dataGridView1 Clicked");
}
Upvotes: 0
Views: 121
Reputation: 928
It could be getting created under the current DataGridView or under some elements within the form. Add a panel to your layout where you would like the DataGridView to be added and try adding it to that Panel and it should work.
Drag and drop a panel from the toolbox on to your form and then in the dataGridView1_Click event, add the control to the panel.
panel1.Controls.Add(dataGridView2);
Upvotes: 1
Reputation: 5772
Hint: This is the code that VS uses when you drag and drop the DGV (adjust as you want)
DataGridView dataGridView1 = new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(dataGridView1)).BeginInit();
//this.SuspendLayout();
dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView1.Location = new System.Drawing.Point(0, 0);
dataGridView1.Name = "dataGridView1";
dataGridView1.Size = new System.Drawing.Size(240, 150);
dataGridView1.TabIndex = 0;
Controls.Add(dataGridView1);
I am not sure why your creating columns manually. Columns would be created automatically for you from the data source when you bind.
Upvotes: 1