2013-03-25 57 views
1

我的DataGridView中的第五列是從我們的SQL服務器中的resumelink中提取信息。文件的名稱是resumelink記錄中的唯一內容,例如。 DOC100.pdfName12.pdf。我需要那些鏈接到計算機上的映射驅動器,所以,如果文件的名稱是DOC100.pdf,它需要是//nt/resume/DOC100.pdf。我需要存儲//nt/resume部分,然後添加resumelink字段中的內容。我有一個名爲dataGridView1_CellContentClick的字段,但它目前是空的。我不關心pdf是如何打開的,無論是在IE還是Adobe。使用Datagridview,獲取現有列並使其成爲超鏈接

下面是一張關於程序外觀的圖片。

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

爲什麼不返回''// nt/resume /'+ resumelink從sql server本身作爲resumelink'? – 2013-03-27 19:40:47

回答

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

附:這不會改變datagrid中文件名的顯示(我認爲它比在一個單元中顯示像//nt/resume/DOC100.pdf這樣的長字符串更好),但會正確處理文件打開。

相關問題