fdsa
fdsa

Reputation: 249

Using Datagridview, taking an existing column and making them hyperlinks

The fifth column in my DataGridView is pulling information from resumelink in our SQL server. The name of the file is the only thing in the resumelink record, eg. DOC100.pdf or Name12.pdf. I need those to link to a mapped drive on the computer so, if the name of the file is DOC100.pdf, it needs to be //nt/resume/DOC100.pdf. I need to store the //nt/resume part and then just add what's in the resumelink field. I have a field called dataGridView1_CellContentClick but it's currently empty. I'm not concerned as to how the pdf's open, whether it's in IE or Adobe.

Here's a picture of how the program looks.

enter image description here

namespace ResumeTest
{
public partial class Resume : Form
{
    SqlConnection conn = new SqlConnection();

    public Resume()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'iHBAPPSDataSet.HRresume' table. You can move, or remove it, as needed.
       this.hRresumeTableAdapter.Fill(this.iHBAPPSDataSet.HRresume);
       this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.White;
       this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Aquamarine;
    }


    private void button1_Click(object sender, EventArgs e)
    {
       bindingSource1.Filter = "name LIKE '%" + name.Text + "%' AND skillset LIKE '%" + skillset.Text + "%'";
    }

    public void ClearTextBoxes(Control control)
    {
        foreach (Control c in control.Controls)
        {
            if (c is TextBox)
            {
                if (!(c.Parent is NumericUpDown))
                {
                    ((TextBox)c).Clear();
                }
            }
            else if (c is NumericUpDown)
            {
                ((NumericUpDown)c).Value = 0;
            }
            else if (c is ComboBox)
            {
                ((ComboBox)c).SelectedIndex = 0;
            }

            if (c.HasChildren)
            {
                ClearTextBoxes(c);
            }
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        ClearTextBoxes(this);
        bindingSource1.Filter = "name LIKE '%" + name.Text + "%'";
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void button4_Click(object sender, EventArgs e)
    {
        Add f2 = new Add();
        f2.Show();
    }

    private void button6_Click(object sender, EventArgs e)
    {
        Delete f3 = new Delete();
        f3.Show();
    }

    private void refreshButton_Click(object sender, EventArgs e)
    {
        this.hRresumeTableAdapter.Fill(this.iHBAPPSDataSet.HRresume);
    }

    private void quitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
  }
}

Upvotes: 1

Views: 2747

Answers (1)

Dmytro
Dmytro

Reputation: 17176

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
      // set the part of filename You have to add to some constant
      // or save it in some external file and read here to be able to edit this value
      // without rebuilding of the project
      const string filePreName = @"//nt/resume/";

      // get the clicked filename value
      string filename = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();

      // combine file pre name and file name
      string finalFilePath = filePreName + filename;

      // this function will start default program, which is configured in your system
      // to handle pdf files and will open selected pdf file in this program
      // to get access to this function you should reference to

      // using System.Diagnostics;

      // at the top of current class file
      Process.Start(finalFilePath);
}

Or put all in one line:

Process.Start(@"//nt/resume/" + dataGridView1[e.ColumnIndex, e.RowIndex].Value);

P.S. This will not change the display of file name in datagrid (which I think is even better than display long string like //nt/resume/DOC100.pdf in one cell) but will handle file opening correctly.

Upvotes: 2

Related Questions