C# DataGridView 获取当前选中的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2084976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGridView get current selected object
提问by Oliver Friedrich
I need to get the currently selected object from da databound DataGridView.
我需要从数据绑定 DataGridView 中获取当前选定的对象。
I do not need the object of the current selected cell, but the object on which the whole row is based, in this case a BusinessObject whos properties make the columns of the grid.
我不需要当前选定单元格的对象,而是整行所基于的对象,在本例中是一个 BusinessObject,其属性构成了网格的列。
I could go over the DataSource, but that itself is just an object and can be a BindingSource or a IBindingList or something like that - so not easy standartized way to get the wanted object.
我可以查看数据源,但它本身只是一个对象,可以是 BindingSource 或 IBindingList 或类似的东西 - 所以不是简单的标准化方法来获取想要的对象。
Behind that is the need to just check the businessObject for a property called IsChanged and ask the user to save or discard the changes, before the bindingsource selects the next item. Therefore I must find out the current object inside RowValidating-Event of the DataGridView, since the BindingSource does not offer an event to stop changing before change occurs.See here for the well known problem
在这之后,只需要检查 businessObject 中名为 IsChanged 的属性,并在 bindingsource 选择下一项之前要求用户保存或放弃更改。因此,我必须在 DataGridView 的 RowValidating-Event 中找出当前对象,因为 BindingSource 不提供在更改发生之前停止更改的事件。请参阅此处了解众所周知的问题
Thanks for reading ;-)
谢谢阅读 ;-)
采纳答案by leppie
DataGridViewRow.DataBoundItem
contains the 'business' object it is bound to.
DataGridViewRow.DataBoundItem
包含它绑定到的“业务”对象。
回答by Kemal Duran
Here is my code put this into your Person class
这是我的代码,将其放入您的 Person 类中
public static explicit operator Person(DataRow dr)
{
Person p = new Person();
p.adi = dr.ItemArray[0].ToString();
p.id = Int32.Parse(dr.ItemArray[1].ToString());
p.soyadi = dr.ItemArray[2].ToString();
p.kartNo = dr.ItemArray[3].ToString();
p.dogumTarihi = DateTime.Parse( dr.ItemArray[4].ToString() );
p.adres = dr.ItemArray[5].ToString();
p.meslek = dr.ItemArray[6].ToString();
p.telefon = dr.ItemArray[7].ToString();
p.gsm = dr.ItemArray[8].ToString();
p.eposta = dr.ItemArray[9].ToString();
return p;
}
and this is a update button click
这是一个更新按钮点击
DataRow row = (dataGridView1.SelectedRows[0].DataBoundItem as DataRowView).Row;
Person selected = (Person)row;
回答by Sarvar Nishonboev
What about this way?
这种方式呢?
foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
{
MessageBox.Show(item.Cells[0].Value.ToString());
}
We can get multiple selected rows data.
我们可以获取多个选定的行数据。
回答by Ken
Since you did state the IBindingList - yes as others have said the DataBoundItem property will give you what you need - there is an issue with it that I had experienced previously and ended up with a null reference but right now I can not think of the scenario in which it happened.
由于您确实声明了 IBindingList - 是的,因为其他人已经说过 DataBoundItem 属性将为您提供您需要的东西 - 我以前遇到过它存在一个问题,并以空引用结束,但现在我想不出这种情况它发生在其中。
If you are databound using a BindingSource - you can capture the CurrentChanged, CurrentItemChanged events of your BindingSource , then you need not have an additional IsChanged Property on your B.O. .. , also the underlying datasource could also indicate modified - for example if you have BindingSource bound to a datatable the row would give you a modified flag.
如果您使用 BindingSource 进行数据绑定 - 您可以捕获 BindingSource 的 CurrentChanged、CurrentItemChanged 事件,那么您的 BO .. 上不需要额外的 IsChanged 属性,基础数据源也可以指示已修改 - 例如,如果您有 BindingSource绑定到数据表的行会给你一个修改的标志。
回答by F.joksch
You can get the selected cell value like this
您可以像这样获取选定的单元格值
yourDGV.CurrentCell.Value;
If you want the value in the form of a String just use ToString() method like this
如果您想要字符串形式的值,只需使用这样的 ToString() 方法
yourDGV.CurrentCell.Value.ToString();
This should do it
这应该做