数据网格视图选择单元格值如何获取它 [英] Data grid view selected cell value how to get it

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

问题描述

我想在Click事件中选择DataGridView Cell值我使用此代码会产生错误



i Want to get DataGridView selected Cell value on click event i use this code its generates a error

DataGridViewRow dgvrows = TrGrid.SelectedRows;
string c = dgvrows.Cells("Late_Time").value.toString();

推荐答案

DataGridView有一个< a href =http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcell(v=vs.80).aspx> CurrentCell属性 [ ^ 。我认为这就是选定单元格的含义。如果你想查看所有选中的单元格,它有一个可枚举的SelectedCells属性来给出单元格对象。
DataGridView has a CurrentCell property[^]. I think that is what you mean by 'selected cell'. If you want to look at all the selected cells, it has a SelectedCells property which is enumerable to give cell objects.


下面这段代码可以帮到你:

The following piece of code will do the trick for you:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
    {
       MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
    }
}





希望这能帮到你。



Hope this will help you out.


SelectedRows 将返回 DataGridViewSelectedRowCollection 类型的行集合,而不是 DataGridViewRow



所以你可以像这样使用它:



SelectedRows will return a collection of rows of type DataGridViewSelectedRowCollection and not DataGridViewRow.

So you can use it like this :

DataGridViewSelectedRowCollection rows = dataGridView1.SelectedRows;
string val =(string)rows[2].Cells["Late_Time"].Value; //I have specified rowIndex as 2 as an example


这篇关于数据网格视图选择单元格值如何获取它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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