我拥有的excel工作表的超链接将定向到用户电脑中的文件夹。我想做的是从Excel将超链接导入到DataGridView
,然后从那里打开超链接指向的文件。但是,我一直看到人们在他们的c#代码中设置超链接网站,这不是我想要的,因为它很难编码。有没有办法实现?
DataGridViewLinkColumn
以使内部具有此行为。示例
private void Form1_Load(object sender, EventArgs e)
{
//Get data
var data = new[] {
new { Title="Stackoverflow", Location = @"https://www.Stackoverflow.com"},
new { Title="Windows Folder", Location = @"C:\Windows"},
new { Title="Network Share", Location = @"\\127.0.0.1"},
};
// Add columns
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn()
{
Name = "TitleColumn", DataPropertyName = "Title", HeaderText = "Title"
});
dataGridView1.Columns.Add(new DataGridViewLinkColumn()
{
Name = "LocationColumn", DataPropertyName = "Location", HeaderText = "Location"
});
//Handle click on link
dataGridView1.CellContentClick += (obj, args) =>
{
if (args.RowIndex < 0 || args.ColumnIndex < 0)
return;
if (dataGridView1.Columns[args.ColumnIndex].Name != "LocationColumn")
return;
var value = $"{dataGridView1[args.ColumnIndex, args.RowIndex].Value}";
if (Uri.TryCreate(value, UriKind.Absolute, out Uri uri))
System.Diagnostics.Process.Start(value);
};
//Show data
dataGridView1.DataSource = data.ToList();
}