2012-01-24 19 views
1

總的noob在這裏,只是玩弄C#(這是非常酷的btw)。我在找什麼形式?在事件表中顯示數據

無論如何,我需要在表格中顯示一些信息。我必須有能力使用代碼動態地設置行和列的內容,並且表中的一個字段必須在C#中觸發事件。

Filename #1   Click here to download 
Filename #2   Click here to download 

你明白了Click here to download應該在C#中觸發一個事件。我在尋找什麼樣的表單類型?我幾乎看過所有這些,但我無法弄清楚哪一個最適合這個。

使用「Windows Form Application」btw。

+0

它被稱爲「控制」。一個'Form'是你放上'Control's的「窗口」。 – ispiro

+0

感謝您清除@ispiro :) – OptimusCrime

回答

2

DataGridView控制基本上是你所描述的。 DataGridView有一個LinkColumn類型,就像一個HTML鏈接。您只需處理CellContentClick事件並確定用戶點擊了哪個單元格:

public Form1() { 
    InitializeComponent(); 

    dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { ReadOnly = true }); 
    dataGridView1.Columns.Add(new DataGridViewLinkColumn()); 

    dataGridView1.Rows.Add(1); 
    dataGridView1.Rows[0].Cells[0].Value = "File #1"; 
    dataGridView1.Rows[0].Cells[1].Value = "Click here"; 

    dataGridView1.CellContentClick += new DataGridViewCellEventHandler(dataGridView1_CellContentClick); 
} 

void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { 
    if (e.ColumnIndex == 1) { 
    MessageBox.Show("Downloading " + dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()); 
    } 
} 
+0

看起來非常棒。今天晚些時候必須嘗試。感謝您的詳細示例! – OptimusCrime

0

您可能想看看ListView s。它們提供了一種顯示元素表的方法,並可以監聽點擊事件。

相關問題