获取单元格

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//获取行号索引

//第一种方式
//int row = e.RowIndex + 1;
//int col = e.ColumnIndex + 1;

//第二种方式
//int row = dataGridView1.CurrentCell.RowIndex + 1;
//int col = dataGridView1.CurrentCell.RowIndex + 1;

//第三种方式
int row = dataGridView1.CurrentCellAddress.Y + 1;
int col = dataGridView1.CurrentCellAddress.X + 1;
int col2 = dataGridView1.CurrentCellAddress.X + 2;

//获取单元格的内容
//第一种方式
//获取第几行第几列的值
string cell2 = dataGridView1.Rows[row - 1].Cells[col - 1].Value.ToString();
string cell3 = dataGridView1.Rows[row - 1].Cells[col2 - 1].Value.ToString();
label1.Text = cell2;
label2.Text = cell3;

//第二种方式
string cell = dataGridView1.CurrentCell.Value.ToString();
MessageBox.Show("您点击的是第" + row.ToString() + "行,第" + col.ToString() + "列\n"+"获取的值是:"+cell);
}

posted @   锦大大的博客呀!  阅读(219)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示