Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HyperLinks In DataGridView

I am working on C# application which is like a small search engine. The user will enter a word and the program will return the files that contains this word.

I have an array of file paths (as strings) and I want to show these paths as links in a DataGridView, so that when the user clicks the file name the file will be opened.

Note: I am working on C# Winforms, not ASP.net

like image 636
Hany Avatar asked Mar 27 '10 17:03

Hany


People also ask

What is DataGridView data binding?

The DataGridView control supports the standard Windows Forms data binding model, so it can bind to a variety of data sources. Usually, you bind to a BindingSource that manages the interaction with the data source.


2 Answers

I think I have The Answer of my question I added a DataGridViewLinkColumn to the DataGridView now the next step will fill the Data into the datagridview and the File names will appear like links:

private void button1_Click(object sender, EventArgs e)
    {
        string[] SS = new string[3];
        SS[0] = "C:\\test1.txt";
        SS[1] = "C:\\test2.txt";
        for (int i = 0; i < SS.Length; i++)
        {
            dataGridView1.Rows.Add(SS[i]);
        }
        dataGridView1.Refresh();
    }

The Last step : now I want to open the file when the user click it I will use the "CellContentClick" event and this code will achieve it:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        string filepath= (string)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
        System.Diagnostics.Process.Start(filepath);
    }
like image 91
Hany Avatar answered Sep 23 '22 11:09

Hany


DataGridViewLinkColumn looks promising.

  • MSDN documentation on DataGridViewLinkColumn
  • Another page describing DataGridViewLinkColumn
like image 40
Zach Johnson Avatar answered Sep 23 '22 11:09

Zach Johnson