2013-03-27 17 views
1

我有一個程序使用一個datagridview,其中有7列。其中一列是將從指定位置加載文件的超鏈接。我使用'cellcontentclick'事件來打開文件。我的問題是,當我單擊行中的其他單元格時,它仍然會執行cellcontentclick。我如何才能做到這一點,只有當特定的列點擊時纔會執行?在c#程序中使用cellcontentclick事件來激活一個鏈接

 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
    { 
     try 
     { 
      string sourcePath = @"SPECIFIED PATH"; 

      Process.Start(sourcePath + dataGridView1.Rows[e.RowIndex].Cells[5].Value); 
     } 

     catch (SqlException e) 
     { 
      MessageBox.Show("Error occured: " + e); 
     } 
    } 

回答

2

僅檢查您要查找的列的內部事件處理程序。 其中一個參數(e?)有列信息。

1

Got it!我只需要輸入if語句並指定列。謝謝,evgenyl。

 if (e.ColumnIndex == 5 && e.RowIndex >= 0) 
     { 
      try 
      { 
       string sourcePath = @"PATH"; 

       Process.Start(sourcePath + dataGridView1.Rows[e.RowIndex].Cells[5].Value); 
      } 

      catch (SqlException a) 
      { 
       MessageBox.Show("Error occured: " + a); 
      } 
     } 
+0

不客氣。如果它有幫助,請將其標記爲答案。 – evgenyl 2013-03-27 17:36:41