簡體   English   中英

C# DataGridView 選擇模式:單元格 Select - 是否可以檢索該選定行的特定列值?

[英]C# DataGridView Selection mode: Cell Select - is it possible to retrieve a specific column value for that selected row?

我一直在為所有 DataGridView 使用FullRowSelect

我會像這樣從 DataGridView 檢索數據:

tableGrid.SelectedRows[0].Cells[6].Value.ToString()

是否可以使用選擇模式: CellSelect並為該特定選定單元格行獲取特定列?

例如,我知道我的第一列( column[0] )是一個id號。 並使用CellSelect在除id列之外的任何列上單擊它,檢索id值。

任何單個DataGridViewCell都將具有行和列索引。 如果選擇模式是CellSelect ,您可以通過幾種不同的方式獲得“選定”單元格的地址。 當網格選擇模式設置為CellSelect或任何其他模式時,網格SelectedCells集合可用...。

DataGridViewCell cell = DataGridView1.SelectedCells[0];

此外,在設置任何選擇模式時,網格都會維護CurrentCell屬性。 哪個“通常”是當前選中的單元格,如果 multi-select 設置為true ,它通常是“最后一個”選中的單元格……

DataGridViewCell cell = DataGridView1.CurrentCell;

然后cell變量有一個RowIndexColumnIndex屬性...

Int rowIndex = cell.RowIndex;
Int colIndex = cell.ColumnIndex;

這是你要問的嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM