網站首頁 編程語言 正文
當前單元格指的是DataGridView焦點所在的單元格,它可以通過DataGridView對象的CurrentCell屬性取得。如果當前單元格不存在的時候,返回null。
取得當前單元格的內容:
object obj = this.dgv_PropDemo.CurrentCell.Value;
注:返回值是object類型的。
取得當前單元格的列Index:
int columnIndex = this.dgv_PropDemo.CurrentCell.ColumnIndex;
取得當前單元格所在的行的Index:
int rowIndex= this.dgv_PropDemo.CurrentCell.RowIndex;
另外,使用DataGridView.CurrentCellAddress屬性來確定單元格所在的行:
int row= this.dgv_PropDemo.CurrentCellAddress.Y;
列:
int column = this.dgv_PropDemo.CurrentCellAddress.X;
注:DataGridView的行和列的索引都是從0開始的。
當前的單元格可以通過設定DataGridView對象的CurrentCell來改變。
DataGridView1.CurrentCell=DataGridView1[int columnIndex,int rowIndex];
注:如果DataGridVIew的選中模式是行選擇,那么會選中當前單元格所在的整行。否則只會選中設置的當前單元格。
將CurrentCell設置為Null可以取消激活的當前單元格。
示例:設置第一行第二列為當前的CurrentCell
this.dgv_PropDemo.CurrentCell = this.dgv_PropDemo[1, 0];
示例:通過向上和向下實現遍歷DataGridView
////// 向上遍歷 /// /// /// private void btn_Up_Click(object sender, EventArgs e) { //獲取上一行的索引 int upRowIndex = this.dgv_PropDemo.CurrentCell.RowIndex - 1; if (upRowIndex < 0) { //選中最后一行 this.dgv_PropDemo.CurrentCell = this.dgv_PropDemo[0, this.dgv_PropDemo.RowCount - 1]; } else { this.dgv_PropDemo.CurrentCell = this.dgv_PropDemo[0, upRowIndex]; } } ////// 向下遍歷 /// /// /// private void btn_Down_Click(object sender, EventArgs e) { //獲取下一行的索引 int nextRowIndex = this.dgv_PropDemo.CurrentCell.RowIndex + 1; if (nextRowIndex > this.dgv_PropDemo.RowCount - 1) { this.dgv_PropDemo.CurrentCell = this.dgv_PropDemo[0, 0]; } else { this.dgv_PropDemo.CurrentCell = this.dgv_PropDemo[0, nextRowIndex]; } }
原文鏈接:https://www.cnblogs.com/dotnet261010/p/6748378.html
相關推薦
- 2022-06-22 C語言詳解判斷相同樹案例分析_C 語言
- 2022-09-30 Python?添加命令行參數步驟_python
- 2022-11-02 Pytorch中DataLoader的使用方法詳解_python
- 2022-01-13 Vite2+TS+el3獲取DOM元素設置類型并進行表單校驗
- 2023-05-18 Kotlin?Extension?Function擴展函數詳細介紹_Android
- 2022-06-26 python中class類與方法的用法實例詳解_python
- 2022-04-18 css實現 快速定位父元素下最后面的幾個子元素,匹配選擇最后幾個子元素
- 2022-04-16 ASP.NET?Core命令行界面CLI用法_基礎應用
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支