我在带有数据集/ SQL数据库的winform中有一个datagridview。 datagridview有一个列,其中的链接指向可以正常使用此事件的网站:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex] is DataGridViewLinkColumn)
{
Process.Start(this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString());
}
}
我现在需要添加一个新的链接列,其中包含本地文件夹中图片的文件名。 由于上面的方法结构,linkClick事件的行为没有区别,但是我需要一个columnlink来打开点击的URL而另一个columnLink打开一个本地文件。 我怎么能这样做?
答案 0 :(得分:0)
您已经可以访问ColumnIndex
...您只需检查当前点击的单元格所属的列:
if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewLinkColumn)
{
var location = Convert.ToString(dataGridView1[e.ColumnIndex, e.RowIndex].Value);
if (e.ColumnIndex == 1)
Process.Start(location);
else if (e.ColumnIndex == 2)
// Open the file specified in 'location'
}