DataGridView中成为空出于某种原因 [英] DataGridView becomes null for some reason

查看:140
本文介绍了DataGridView中成为空出于某种原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET 2.0框架ASP.NET应用程序。

I have a NET 2.0 framework ASP.NET application ..

我遇到的麻烦的页面上有一个按钮( B1 ),并低于一个DataGridView( DGV ),然后低于第二个按钮( B2 )..

The page I'm having trouble with has a button (B1), and the below that a DataGridView (DGV), and then below that a second button (B2) ..

最初仅B1是可见的。当用户点击B1,我做了一些数据库查询,获取一些数据,把它放在一个数据表,然后设置数据源的DGV的这个数据表.. B1也使得DGV和B2可见,而这两者都是无形之前。现在用户填写了一些数据在现在可见的DGV和点击B2 ..

Initially only B1 is visible. When user clicks B1, I do some DB queries, fetch some data, put it in a DataTable, and then set the DataSource of that DGV to this DataTable .. B1 also makes DGV and B2 visible, both of which were invisible before .. Now user fills in some data in the now-visible DGV and clicks B2 ..

问题是,在B2的code,如果我尝试地看到,DGV的数据源,其空!? ..我需要访问该DGV更新的值..我该怎么办呢?

Problem is that in B2's code, if I try to see the DataSource of that DGV, its null !? .. I need to access the updated values in that DGV .. How do I do that ?

我试过了几个黑客,但似乎没有任何工作。我已经排序得到它通过的FindControl 的工作,但我不希望使用因为我需要其他原因(这里没有提及,因为他们是相当复杂)。

I've tried a few hacks, but nothing seems to work .. I've sort of got it working through FindControl, but I don't want to use that because I need the original DataTable for other reasons (not mentioned here because they're quite complex) ..

code,设定DataSource的数据表:

epsDGV.DataSource = epsDataDT; // epsDataDT是数据表:

code,获取数据表:

的DataTable epsDataDT2 =(数据表)epsDGV.DataSource; //返回null

推荐答案

数据源是不是回发期间可用。它是当前页面的生命周期过程中使用,以填补每一行的相关数据的网格,然后被丢弃。

DataSource is not available during a postback. It is used during the current page lifecycle to fill the grid with the relevant data for each row, and is then discarded.

如果它不这样做的话,那就将它发送回客户端的视图状态中。既然你可能已经递到包含成千上万的记录列表(仅其中的一些可能会显示,取决于寻呼设置),它会造成的巨大的膨胀的视图状态 - 所有这一切都需要被发送到客户端,然后备份到服务器,当你点击B2。

If it didn't do this, then it would need to send it back to the client inside the viewstate. Given that you could have handed it a list containing thousands of records (only some of which it might be showing, depending on paging settings), it would cause huge bloat to the viewstate - all of which needs to be sent to the client, then back up to the server when you clicked on B2.

相反,网格控制将跟踪填充到其列中的数据。这将包括内嵌编辑过程中所做的更改。

Instead, the grid control will keep track of the data populated into its columns. This will include the changes made during inline editing.

在网上,我发现大多数例子建议使用的FindControl来获得新值回,并使用ViewState的存储(和检索)数据源 - 如果你真的需要保存它。不过,我的的建议反对这样做,除非你能保证数据源是非常小的 - 否则你就有可能呛客户端的带宽(特别是如果他们是在一个缓慢的连接)

Most examples online I've found suggest using FindControl to get the new value back, and using ViewState to store (and retrieve) the datasource - if you really do need to save it. However, I would highly recommend against doing that unless you can guarantee that the datasource is very small - otherwise you risk choking the clients bandwidth (particularly if they are on a slow connection)

如果您需要将其存储在ViewState中,只需创建类似下面的属性(注:您的数据源对象需要是可序列化,否则将无法获得存储在ViewState)

If you need to store it in Viewstate, just create a property like below (NB: your datasource object needs to be serializable, or it won't be able to get stored in the ViewState):

public List<Items> Items
{
    get 
    {
        if (ViewState["Items"] != null)
            return (List<Items>)ViewState["Items"];
        else
            return null;
    }
    set { ViewState["Items"] = value; }
}

然后在你的数据加载方法设置。在回发,你可以致电酒店拉回来了视图状态。

then set it during your Data Load methods. On postback, you can call the property to pull it back out of the Viewstate.

这篇关于DataGridView中成为空出于某种原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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