我如何获得GridView的底层数据源? [英] How do I get Gridview underlying datasource?

查看:69
本文介绍了我如何获得GridView的底层数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GridView的框架下3.5我的asp.net web应用程序。我绑定与列表中的GridView控件。网格内有更新和删除它们运行良好功能。但我的保存功能,为此,我决定提取列表从数据源回来,然后通过循环,我将插入新的列表数据库。但是,当我试图得到它传统的方式,它返回我空。

i have gridview in my asp.net web application under framework 3.5. I am binding the gridview with List. Inside the grid there is update and delete functionalities which are running fine. But my save functionality, for which i decided to extract the list back from the datasource and then through loop i will insert the new list to database. But when i tried to get it conventional way it returned me null.

我尝试以下方法来检索列表返回。

I tried the following ways to retrieve the List back.

1. List<MyClass> list = (List<MyClass>gv.DataSource);
2. List<MyClass> list = gv.DataSource as List<MyClass>;
3. IDataSource idt = (IDataSource)gv.Datasource;
   List<MyClass> list = (List<MyClass>)idt;

但没有运气,每次我得到空。

But no luck, each time i got null.

推荐答案

您不能retreive数据源一旦被绑定和页面早餐。你必须提供给你几个方法,虽然保留了数据源:

You cannot retreive the datasource once is is bound and the page is served. You have a few methods available to you to retain the datasource though:


  1. 在绑定之前存储数据的会话

  2. 在绑定之前存储数据的的ViewState

  3. 获取从数据库或其他数据存储从最初检索的数据。

  4. 保留存储在其他地方变化的持续高速缓存(如会话的ViewState 等)

  1. Store the data before binding in the Session
  2. Store the data before binding in the ViewState
  3. Fetch the data from the DB or whatever data store you retrieved it from originally.
  4. Keep an ongoing cache of changes stored somewhere else (eg Session, ViewState, etc)

我preFER从阻力远离拖放数据源useage虽然绑定数据。

I prefer to stay away from drag and drop useage of datasources and binding data though.

所以你的情况存储列表某处访问和操纵它,当您去,每次重新绑定。然后,当你想要做的拯救你可以处理基础数据对象(列表),您已存储和被用来定义GUI。在 GridView控件不是一个数据存储,只需控制present基于数据存储中的数据。

So in your case store the list somewhere accessible and manipulate it as you go along and rebind it each time. Then when you want to do that 'save' you can just deal with the underlying data object (List) that you have stored and is being used to define the GUI. The GridView is not a datastore, just a control to present the data based on a data store.

这篇关于我如何获得GridView的底层数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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