2013-04-08 99 views
0

如果是這樣,我們如何分別處理cellcontentclick事件?我正在動態添加列。繼承人我的代碼: -
dataGridViewShow.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "Date", DataPropertyName = "Date", CellTemplate = new DataGridViewTextBoxCell() });datagridview中可以有兩個不同類型的鏈接列嗎?

DataGridViewLinkColumn toComputer = new DataGridViewLinkColumn(); 
     toComputer.HeaderText = "Status"; 
     toComputer.Name = "link1"; 
     toComputer.UseColumnTextForLinkValue = true; 
     toComputer.Text = "Save to File"; 
     toComputer.ActiveLinkColor = Color.White; 
     toComputer.LinkColor = Color.Blue; 
     toComputer.TrackVisitedState = true; 
     toComputer.VisitedLinkColor = Color.Green; 
     dataGridViewShow.Columns.Add(toComputer); 
     dataGridViewShow.CellContentClick += (SaveFileType_click); 

DataGridViewLinkColumn toNet = new DataGridViewLinkColumn(); 
     toLinx.HeaderText = " "; 
     toLinx.Name = "link2"; 
     toLinx.UseColumnTextForLinkValue = true; 
     toLinx.Text = "Send to Net"; 
     toLinx.ActiveLinkColor = Color.White; 
     toLinx.LinkColor = Color.Blue; 
     toLinx.TrackVisitedState = true; 
     toLinx.VisitedLinkColor = Color.Green;    
     dataGridViewShow.Columns.Add(toNet); 
     dataGridViewShow.CellContentClick += ("Don't know how this gets handled"); 

上面的代碼確實顯示鏈接類型的兩個不同的列,但我想不通我怎麼分別處理每個事件?

private void SaveFileType_click(object sender, DataGridViewCellEventArgs e) 
    { 
     selectedRow = e.RowIndex; 

     if (e.ColumnIndex == 0) 
     { 
      //some code goes here 
     }if (e.ColumnIndex == 1 && (string)dataGridViewShow[e.ColumnIndex, e.RowIndex].Value != "Complete") 
     { 
      //some code goes here 
     } 
    } 

回答

2

您不單獨處理事件。您需要處理OnCellClickCellContentClick事件。然後使用DataGridViewCellEventArgs對象e,您可以獲取被點擊的列和行。基於這些細胞被選中你處理這個事件不同

public void dataGridView1_OnCellClick(object sender, 
    DataGridViewCellEventArgs e) 
{ 
    if (e.ColumnIndex == YourLinkColumnIndexA) 
     //some code goes here. 
    else if (e.ColumnIndex == YourLinkColumnIndexB) 
     //some code goes here. 
} 

所以你連線了事件中的代碼爲你做,但只有一次

我希望這會有所幫助。

+0

它的確幫助了謝謝。 – smons 2013-04-08 21:17:24

相關問題