Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 强制转换为databounditem值的对象_C#_Linq - Fatal编程技术网

C# 强制转换为databounditem值的对象

C# 强制转换为databounditem值的对象,c#,linq,C#,Linq,嗨,如果我将datatable中的结果绑定到gridview,我如何从该gridview获取databounditem?以下代码失败,因为它无法强制转换到对象 private void gvInv_MouseUp(object sender, MouseEventArgs e) { if (null == gvInv.CurrentRow) return; inventory = (Inventory)gvInv.CurrentRow.DataBoun

嗨,如果我将datatable中的结果绑定到gridview,我如何从该gridview获取databounditem?以下代码失败,因为它无法强制转换到对象

  private void gvInv_MouseUp(object sender, MouseEventArgs e)
    {
        if (null == gvInv.CurrentRow) return;
        inventory = (Inventory)gvInv.CurrentRow.DataBoundItem;

请注意。

从评论中可以看出,
DataBoundItem
是一个
DataRowView
,因此我假设您绑定了一个类型化的数据集,
Invoice
是您的专用
DataRow

在这种情况下,您需要的对象是行视图的
.row
,即

var rowView = gvInv.CurrentRow.DataBoundItem as DataRowView;
if(rowView != null)
{
    inventory = rowView.Row as Inventory;
}

DataBoundItem的作用是什么。声称在调试器中?当我选择dataBoundItem时,我希望它会返回该对象中的所有属性-库存。是的,但是当您通过
gvInv\u MouseUp
中的断点查看调试器中的
gvInv.CurrentRow.dataBoundItem
时,它是什么?这是关键…这是系统数据。DataRowView@Marc格雷威尔:嗨,马克,这是系统数据。DataRowView@MarcGravell:我在行
rowView.Row as Inventory
错误3无法通过引用转换、装箱转换、取消装箱转换、包装转换或空类型转换将类型“System.Data.DataRow”转换为“le.DataModel.Inventory”“@belinq-嗯,
Inventory
是一个类型化的数据行,还是完全无关?@Marc gravel:Inventory是一个类对象。我在DataModel调用中编写了一个类调用Inventory.csInventory@belinq-对,那么您已经绑定到一个
数据表
,但是您想要提取
库存
对象,它们是完全不相关的,对吗?那不是演员阵容,那是一个皈依者。这就是你想要做的吗?@Marc Gravell:谢谢你的回复,我想库存对象与我选择的行相关。让我澄清一下我的情况,我首先从Inventory表获取所有库存,并使用dataTable绑定到gvInv。这是我的代码,bsInv.DataSource=linqHelper.ToDataTable(inventory.Get()作为列表);gvInv.DataSource=bsInv;然后在MouseUp()上,我想执行inventory=(inventory)gvInv.CurrentRow.DataBoundItem;