持久化 DataTable 或 GridView 数据源

发布于 2024-12-13 23:39:19 字数 1360 浏览 1 评论 0原文

我有一个填充 DataTable 的 Click 事件,而 DataTable 是我的 GridView 的源。

然后我有另一个点击事件尝试获取 GridView DataSource e 将其转换回 DataTable,如下所示:

DataTable dt = (DataTable)GridView1.DataSource;

但 Datasource 返回 null。如果我将代码和 Page_Init 事件等待正确的回发,则会发生事件,

所以我想知道如何保留 gridview 的数据源或

根据需要编辑的 DataTable:

这是整个代码:

ps:Page_Init 是另一个尝试获取数据源

    private DataTable _dataTable;

    public DataTable dataTable
    {
        get { return _dataTable; }
        set { _dataTable = value; }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        if(Page.IsPostBack)
        {
            string ctrlname = BLL.Common.GetPostBackControlId(this.Page);

            if(ctrlname == "ButtonDownload")
            {
                DataTable dt = (DataTable)GridView1.DataSource;
            }
        }
    }


    protected void Filter_Click(object sender, EventArgs e)
    {
        string[] status = new string[2];
        status[0] = "Paga";
        status[1] = "Disponivél";

        dataTable = BLL.PagSeguro.GetTransactions(TextBoxInicio.Text, TextBoxFim.Text, status);

        GridView1.DataSource = dataTable;
        GridView1.DataBind();
    }


    protected void GetDataSource(object sender, EventArgs e)
    {
        DataTable dt = (DataTable)GridView1.DataSource;
    }

I have a Click Event that fills a DataTable and the DataTable is the source of my GridView.

Then I have another click event that tries to get the GridView DataSource e converts it back to a DataTable Like:

DataTable dt = (DataTable)GridView1.DataSource;

But the Datasource returns null. Event if I put the code and the Page_Init event waiting for the right postBack

so I would like to know how can i persist the datasource of the gridview, or the DataTable

edited as required:

here is the whole code:

ps: the Page_Init was another try to get the datasource

    private DataTable _dataTable;

    public DataTable dataTable
    {
        get { return _dataTable; }
        set { _dataTable = value; }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        if(Page.IsPostBack)
        {
            string ctrlname = BLL.Common.GetPostBackControlId(this.Page);

            if(ctrlname == "ButtonDownload")
            {
                DataTable dt = (DataTable)GridView1.DataSource;
            }
        }
    }


    protected void Filter_Click(object sender, EventArgs e)
    {
        string[] status = new string[2];
        status[0] = "Paga";
        status[1] = "Disponivél";

        dataTable = BLL.PagSeguro.GetTransactions(TextBoxInicio.Text, TextBoxFim.Text, status);

        GridView1.DataSource = dataTable;
        GridView1.DataBind();
    }


    protected void GetDataSource(object sender, EventArgs e)
    {
        DataTable dt = (DataTable)GridView1.DataSource;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里南柯 2024-12-20 23:39:19

这可能对你有用。

public partial class Demo : System.Web.UI.Page
{
    private DataTable _myData = null;
    protected DataTable MyData
    {
        get
        {
            if (null == _myData)
            {
                // You would load your data here.
                _myData = new DataTable();
            }
            return _myData;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Lets say you set your data source here
        myGrid.DataSource = this.MyData;
    }

    protected void Rendering(object sender, EventArgs e)
    {
        // This is some other event that also needs to get at the data.
        DataTable mydata = this.MyData;
    }

    protected void Unload(object sender, EventArgs e)
    {
        if (null != _myData)
        {
            _myData.Dispose();
            _myData = null;
        }
    }

This might work for you.

public partial class Demo : System.Web.UI.Page
{
    private DataTable _myData = null;
    protected DataTable MyData
    {
        get
        {
            if (null == _myData)
            {
                // You would load your data here.
                _myData = new DataTable();
            }
            return _myData;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Lets say you set your data source here
        myGrid.DataSource = this.MyData;
    }

    protected void Rendering(object sender, EventArgs e)
    {
        // This is some other event that also needs to get at the data.
        DataTable mydata = this.MyData;
    }

    protected void Unload(object sender, EventArgs e)
    {
        if (null != _myData)
        {
            _myData.Dispose();
            _myData = null;
        }
    }
海拔太高太耀眼 2024-12-20 23:39:19

我很确定您只能通过 DataBound 事件或 ItemDataBound 事件访问数据源。您也许能够访问 Items 集合中每个项目的 DataRowView,但我不确定:

DataRow row = ((DataRowView)GridView1.Rows[0].DataItem).Row;

至于保留数据源,您需要考虑这是否是一个好主意。存储数据源的选项是 SessionCache,但如果结果集相当小,那么当您需要数据源时进行另一次往返可能会更有效。无论您决定做什么,都不要将其存储在 ViewState 中。

I'm pretty sure you can only access the datasource that way through the DataBound event or ItemDataBound event. You might be able to access the DataRowView for each item in the Items collection, but I'm not sure:

DataRow row = ((DataRowView)GridView1.Rows[0].DataItem).Row;

As for persisting the datasource, you need to consider whether that's a good idea. Your options for storing the datasource are Session or Cache, but if the result set is fairly small it might be more efficient to make another round trip when you need the datasource. Whatever you decide to do, don't store it in ViewState.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文