【问题标题】:How to get some cell in last row of datagridview?如何在datagridview的最后一行获取一些单元格?
【发布时间】:2012-09-07 09:06:05
【问题描述】:

我有如下表

这些是我正在尝试做的代码,但没有希望,我想获得 CAid014

string splitter = dataGridView1.Rows[dataGridView1.RowCount].Cells[0].ToString();
MessageBox.Show(splitter); // to print what splitter got

我正在使用 datagridview1.rowcount,因此 datagridview1.rows 将获得最后一行。并且单元格[0] 将获得行的 0 索引并且它得到了错误。

我如何获得 CAid 单元格?


已解决:我想念这样的价值

 string splitter = dataGridView1.Rows[dataGridView1.RowCount].Cells[0].value.ToString();

【问题讨论】:

  • 你试过dataGridView1.Rows[dataGridView1.RowCount - 1] ??
  • @yogi 是的,我做到了,mbox 没有显示 CAid014
  • @Anonymous 我想获得 CAid014。如果我使用单元格 [0] 是否正确?
  • @nencor:- 看看你是否使用 Rows 它将返回 Row 集合,因此添加 [X] 将指向集合中的第 X 个索引。所以 cells[0] 显然返回第一个单元格的内容在最后一行。所以最好使用 String.Format("{0}",dataGridView1.Rows[dataGridView1.RowCount-1].Cells[0].Value)。也许还有 value2 属性我几乎不记得了;你可以试试
  • @nencor 如果我们帮助你,你应该接受一个答案

标签: c# datagridview rows cells


【解决方案1】:
dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[0].Value.ToString();

.Rows是一个集合,也像数组一样工作,arrays的索引编号从0到N-1

【讨论】:

  • 它返回“DatagridviewTextboxcell { ColumnIndex=0, RowIndex=3 }” 我想获得CAid014,我该如何获得?
  • @nencor 使用 Cells[0].value .
【解决方案2】:

一个更简单的方法是:-

string splitter = (String)dataGridView1[ 0,dataGridView1.RowCount - 1].Value;

MessageBox.Show(splitter);

【讨论】:

    【解决方案3】:

    datagridview 中的Row Collection 是基于 0 的索引。所以我同意dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[0].ToString();

    【讨论】:

    • 你需要使用value来返回单元格的值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    相关资源
    最近更新 更多