【问题标题】:dynamically created datagridview cell click event does not work动态创建的datagridview单元格点击事件不起作用
【发布时间】:2012-12-19 15:16:35
【问题描述】:

我正在动态创建 dgv,我必须在其单元格单击时创建一个事件,我该怎么做? 现在,当我创建 cellclick 事件时,它什么也不触发。

制作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);

我的 cellClick 事件

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
    {
    }
}

但是什么都没有发生。

【问题讨论】:

  • 可能是autopostback= true 也应该为Gridview设置?
  • 我在 Windows 窗体中工作,而不是在网站应用程序中。
  • 当我不使用动态 dgv 时,相同的代码正在工作。但由于我的代码,我必须使用动态创建的 dgv。

标签: .net c#-4.0 datagridview


【解决方案1】:
dataGridView2.CellClick += new DataGridViewCellEventHandler(dataGridView2_CellClick);

成功了。>:)

【讨论】:

    猜你喜欢
    • 2012-09-27
    • 2023-03-21
    • 2015-09-25
    • 2021-02-07
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    相关资源
    最近更新 更多