使用Datagridview,获取现有列并使其成为超链接

时间:2013-03-25 15:10:07

标签: c# datagridview hyperlink

我的DataGridView中的第五列是从我们的SQL服务器中的resumelink中提取信息。文件名是resumelink记录中唯一的内容,例如。 DOC100.pdf Name12.pdf 。我需要链接到计算机上的映射驱动器,因此,如果文件名是 DOC100.pdf ,则需要//nt/resume/DOC100.pdf。我需要存储//nt/resume部分,然后只需添加resumelink字段中的内容即可。我有一个名为dataGridView1_CellContentClick的字段,但它目前是空的。我不关心pdf如何打开,无论是在IE还是Adobe。

以下是该程序的外观图片。

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();
    }
  }
}

1 个答案:

答案 0 :(得分:2)

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);
}

或者将所有内容放在一行:

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

P.S。这不会改变datagrid中文件名的显示(我认为这比在一个单元格中显示像//nt/resume/DOC100.pdf这样的长字符串更好),但会正确处理文件打开。