XPTable 是一套開源的 WinForms 表格控制項,功能強大但文件較少。本文記錄 `CellClick` 事件中取得點擊儲存格文字的正確寫法。
private void tableXP_CellClick(object sender, XPTable.Events.CellMouseEventArgs e)
{
// e.Row: 點擊的列索引
// e.Column: 點擊的欄索引
// e.Cell: 被點擊的 Cell 物件 (可為 null)
if (e.Cell == null) return; // 點擊空白處或標題列
// 方式 1: 直接取 Cell.Text (最簡單)
string cellText = e.Cell.Text;
// 方式 2: 透過 TableModel 取得 (同樣結果)
// string cellText = this.tableXP.TableModel.Rows[e.Row].Cells[e.Column].Text;
// 範例:只處理第 9 欄 (索引 8) 的點擊
if (e.Column == 8)
{
MessageBox.Show($"第 {e.Row + 1} 列、第 {e.Column + 1} 欄內容: {cellText}");
// 後續業務邏輯...
}
}
var row = tableXP.TableModel.Rows[e.Row];
string col0 = row.Cells[0].Text;
string col1 = row.Cells[1].Text;
// 或轉為強型別物件 (若使用 RowObjects)
var myObj = row.Tag as MyDataObject;