DataGridView使用技巧一:获取或设置当前单元格的内容
当前单元格指的是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
/// <summary>
/// 向上遍历
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Up_Click(object sender, EventArgs e)
{
//获取上一行的索引
int upRowIndex = this.dgv_PropDemo.CurrentCell.RowIndex - ;
if (upRowIndex < )
{
//选中最后一行
this.dgv_PropDemo.CurrentCell = this.dgv_PropDemo[, this.dgv_PropDemo.RowCount - ];
}
else
{
this.dgv_PropDemo.CurrentCell = this.dgv_PropDemo[, upRowIndex];
}
} /// <summary>
/// 向下遍历
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Down_Click(object sender, EventArgs e)
{
//获取下一行的索引
int nextRowIndex = this.dgv_PropDemo.CurrentCell.RowIndex + ;
if (nextRowIndex > this.dgv_PropDemo.RowCount - )
{
this.dgv_PropDemo.CurrentCell = this.dgv_PropDemo[, ];
}
else
{
this.dgv_PropDemo.CurrentCell = this.dgv_PropDemo[, nextRowIndex];
}
}
DataGridView使用技巧一:获取或设置当前单元格的内容的更多相关文章
- DataGridView获取或者设置当前单元格的内容
当前单元格指的是DataGridView焦点所在的单元格,它可以通过DataGridView对象的CurrentCell属性取得.如果当前单元格不存在的时候,返回null. 取得当前单元格的内容: o ...
- C#中的Excel操作【1】——设置Excel单元格的内容,打开Excel文件的一种方式
前言 作为项目管理大队中的一员,在公司里面接触最多的就是Excel文件了,所以一开始就想从Excel入手,学习简单的二次开发,开始自己的编程之路! 程序界面 功能说明 打开文件按钮,可以由使用者指定要 ...
- wpf 获取DataGrid某一个单元格,设置此单元格ToolTip内容和背景颜色
public void GetCell() { for (int i = 0; i < this.datagrid1.Items.Count; i++) ...
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
一.单元格内容的操作 *****// 取得当前单元格内容 Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 Index ...
- 转:c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
一.单元格内容的操作 *****// 取得当前单元格内容 Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 Index ...
- DataGridView中EnditCommit()调用之后,单元格的内容被全选了,每次输入都要鼠标点击定位到最后才能继续输入
因为某些需求,DataGridView在输入一次内容,就要调用ECommitEdit(DataGridViewDataErrorContexts.Commit)来将内容提交,但是这样做之后,控件就会当 ...
- c#DataGridView数据绑定示例——格式化单元格的内容(转)
转自http://blog.csdn.net/testcs_dn/article/details/37834063 c#DataGridView数据绑定示例 格式化单元格的内容 在使用DataGrid ...
- selenium获取html的表格单元格数据
获取网页的表格的某个单元格的值,思路: 1.获取表格 2.获取表格的所有行 3.根据某一行获取该行的所有列 4.根据某一列获得该行该列的单元格值 根据以上思路,可以知道,只需要行.列就可以得到单元格的 ...
- POI按照源单元格设置目标单元格格式
原文:http://jjw198874.blog.163.com/blog/static/1889845522011102401854234/ POI按照源单元格设置目标单元格格式 poi按照一个源单 ...
随机推荐
- 【LeetCode】93. Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- php 内存管理
内存是计算机⾮常关键的部件之⼀,是暂时存储程序以及数据的空间,CPU只有有限的寄存器可以⽤于存储计算数据,⽽⼤部分的数据都是存储在内存中的,程序运⾏都是在内存中进⾏的.和CPU计算能⼒⼀样, 内存也是 ...
- 总结PLSQL的快捷键以及使用技巧
http://www.dedecms.com/knowledge/data-base/oracle/2012/0724/3643.html 最近在开发过程中,遇到一些麻烦,就是开发效率问题,有时候其他 ...
- SQL SERVER SELECT语句中加锁选项的详细说明 [转]
SQL Server提供了强大而完备的锁机制来帮助实现数据库系统的并发性和高性能.用户既能使用SQL Server的缺省设置也可以在select 语句中使用“加锁选项”来实现预期的效果. 本文介绍了S ...
- Hadoop命令手册
原文地址:http://hadoop.apache.org/docs/r1.0.4/cn/commands_manual.html 概述 常规选项 用户命令 archive distcp fs fsc ...
- Python练习笔记——斐波那契数列
斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一 ...
- python学习笔记011——内置函数sorted()
1 描述 sorted() 函数对所有可迭代的对象进行排序操作. sorted() 与sort()函数之间的区别 1 排序对象 sorted:所有可迭代对象的排序 sort:list列表的排序 2 返 ...
- Microsoft Excel不能访问文件
Microsoft Excel 不能访问文件"C:\Users\james\Documents\test.xls". 可能的原因有以下几个: • 文件名称或路径不存在. • 文件正 ...
- mysql - 语法复习与学习
//本月的第一天,最后一天 $start=date('Y-m-01', strtotime(date("Y-m-d"))); echo date('Y-m-d', strtotim ...
- jquery中常见问题及解决办法小结
1 在开发开放聊天室的过程中,遇到使用ajax提交表单插入数据库时会插入两条数据的情况 解决办法,在ajax函数返回后,return false. $("#btn").click( ...