如何从objectdatasource获取数据集列值

时间:2022-08-25 23:23:51

It has been so long since I've worked with webforms I've forgot how to gain access to the underlying data to get the column value so that I can set the current request for a specific product to the page title.

自从我使用webforms以来,我已经忘记了如何访问底层数据以获取列值,以便我可以将特定产品的当前请求设置为页面标题。

I've got a detailsview on a webforms page and I need to get the value of a column named Title.

我在webforms页面上有一个详细信息视图,我需要获取名为Title的列的值。

How should I go about doing this?

我该怎么做呢?

I'm sure it is because I'm in the Page_Load event.

我确定这是因为我在Page_Load事件中。

Any suggestions?

   protected void Page_Load(object sender, EventArgs e)
{
    DataSet ds = GetDS(this.ObjectDataSourceItem);
    if (ds != null)
    {
        string title = ds.Tables[0].Columns["Title"].ToString();
        this.Page.Title = title;
    }
}
private DataSet GetDS(ObjectDataSource ods)
{
    DataSet ds = new DataSet();
    DataView dv = (DataView)ods.Select();
    if (dv != null && dv.Count > 0)
    {
        DataTable dt = dv.ToTable();
        ds.Tables.Add(dt);
    }
    return ds;
}

My error is Invalid cast exception.

我的错误是无效的强制转换异常。

Here is a snapshot of my error: 如何从objectdatasource获取数据集列值

这是我的错误的快照:


The correct way to gain access to the underlying object, my custom object name Item.

获取对底层对象的访问权限的正确方法,我的自定义对象名称Item。

   private Item GetItem(ObjectDataSource ods)
{
    Item itm = null;
    object[] iObjs = (object[])ods.Select();

    for (int i = 0; i < iObjs.Length; i++)
    {
        itm = (Item)iObjs[i];
    }
    return itm;
}

1 个解决方案

#1


0  

From MSDN: ObjectDataSource.Select doesn't have to return a DataView type. From the method signature, it returns an IEnumerable. From your debugger screenshot, you can see that it is an array of objects. You just need to iterate over the array to get to the value buried underneath.

从MSDN:ObjectDataSource.Select不必返回DataView类型。从方法签名中,它返回一个IEnumerable。从调试器屏幕截图中,您可以看到它是一个对象数组。你只需要迭代数组就可以获得埋在下面的值。

#1


0  

From MSDN: ObjectDataSource.Select doesn't have to return a DataView type. From the method signature, it returns an IEnumerable. From your debugger screenshot, you can see that it is an array of objects. You just need to iterate over the array to get to the value buried underneath.

从MSDN:ObjectDataSource.Select不必返回DataView类型。从方法签名中,它返回一个IEnumerable。从调试器屏幕截图中,您可以看到它是一个对象数组。你只需要迭代数组就可以获得埋在下面的值。