0

我有一个 gridview 通过类和代码 id 的方法加载带有代码的列:

            Queue objQueue = new Queue();
            dataGridView1.DataSource = objQueue.GetAllQueue();
            DataGridViewButtonColumn btnFile = new DataGridViewButtonColumn();
            btnFile.UseColumnTextForButtonValue = true;
            btnFile.Name = "btnViewFile";
            btnFile.Text = "View";
            btnFile.HeaderText = "View Image";
            dataGridView1.Columns.Add(btnFile);

在数据网格视图中加载按钮没有问题,但我如何绑定此按钮以通过新设计的表单或 Windows 图像和传真查看器打开图像链接。

4

3 回答 3

0

您可以将事件处理程序附加到 DataGrid 视图

dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);

并将其处理为

void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0) //Or your Button Index location
            {
                MessageBox.Show("Here");
            }
        }

然后,您可以创建一个新表单并显示它或按照您的意愿打开一个图像链接。

于 2012-09-27T11:00:35.060 回答
0

解决方案 :

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex ==1) //Assuming the button column as second column, if not can change the index
            {
                //check if anything needs to be validated here
                Form1 f = new Form1();
                f.navId = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
                f.Show();

            }
        }

在上面的代码中,使用这个语句分配 id 的值 f.navId = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();

为此,只需在目标表单中声明一个公共变量(例如,navId)

于 2012-09-27T10:46:04.910 回答
0

这应该工作

btnFile.Click += btnFile_Click;

现在定义 btnFile_Click 文件以在响应中流式传输图像

于 2012-09-27T10:31:41.330 回答