Paz
Paz

Reputation: 694

dynamically created datagridview cell click event does not work

I am dynamically creating dgv and i have to make a event on its cell click, how can i do this? Right now when i create the cellclick event, it fires nothing.

Code to make dgv:

               dataGridView2 = new DataGridView();

                DataGridViewColumn col1 = new DataGridViewImageColumn
                {
                    ImageLayout = DataGridViewImageCellLayout.Stretch
                };

                DataGridViewColumn col2 = new DataGridViewImageColumn
                {
                    ImageLayout = DataGridViewImageCellLayout.Stretch
                };

                DataGridViewColumn col3 = new DataGridViewTextBoxColumn();
                DataGridViewColumn col4 = new DataGridViewLinkColumn();
                DataGridViewColumn col5 = new DataGridViewTextBoxColumn();
                DataGridViewColumn col6 = new DataGridViewTextBoxColumn();
                DataGridViewColumn col7 = new DataGridViewTextBoxColumn();

                col1.HeaderText = "TPM Image";
                col1.Name = "image_tpm";
                col1.Width = 60;

                col2.HeaderText = "Find Image";
                col2.Name = "image_thefind";
                col2.Width = 60;

                col3.HeaderText = "Name";
                col3.Name = "name";
                col3.Width = 150;

                col4.HeaderText = "URL";
                col4.Name = "product_url";
                col4.Width = 100;

                col5.HeaderText = "Price";
                col5.Name = "price";
                col5.Width = 70;

                col6.HeaderText = "Accuracy";
                col6.Name = "image_accuracy";
                col6.Width = 52;

                col7.HeaderText = "History";
                col7.Name = "history";
                col7.Width = 200;

                dataGridView2.Columns.Add(col1);
                dataGridView2.Columns.Add(col2);
                dataGridView2.Columns.Add(col3);
                dataGridView2.Columns.Add(col4);
                dataGridView2.Columns.Add(col5);
                dataGridView2.Columns.Add(col6);
                dataGridView2.Columns.Add(col7);

                dataGridView2.Location = new System.Drawing.Point(650, 20);
                dataGridView2.Size = new System.Drawing.Size(702, 413);
                this.Controls.Add(dataGridView2);

My cellClickEvent

private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
    try
    {
        if (dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Contains("http:"))
        {
            Process.Start(dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
        }
    }
    catch
    {
    }
}

But nothing is happening.

Upvotes: 1

Views: 2451

Answers (1)

Paz
Paz

Reputation: 694

dataGridView2.CellClick += new DataGridViewCellEventHandler(dataGridView2_CellClick);

Got worked.>:)

Upvotes: 5

Related Questions