如何选择一个单元格的值? [英] How to select the value of a cell?

查看:103
本文介绍了如何选择一个单元格的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGridView的,我要选择的第一列的单元格。

I have a DataGridView, I want to select the cell of the first column.

继承人是我的 datagridview.Click 方法:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    name = dataGridView1.CurrentRow.Cells[0].Value.ToString();
}

目前我的名字变量

我在做什么错

推荐答案

CurrentRow可能没有设置然而,所以使用RowIndex属性的事件的说法。试着这样说:

CurrentRow may not be set yet, so use the RowIndex property for the event argument. Try it this way:

void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
  if (e.RowIndex > -1 && dataGridView1.Rows[e.RowIndex].Cells[0].Value != null) {
    name = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
  }
}

和为了以防万一,确保该事件是有线达:

And just in case, make sure the event is wired up:

public Form1() {
  InitializeComponent();
  dataGridView1.CellClick += dataGridView1_CellClick;
}

这篇关于如何选择一个单元格的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆