持久化 DataTable 或 GridView 数据源
我有一个填充 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能对你有用。
This might work for you.
我很确定您只能通过
DataBound
事件或ItemDataBound
事件访问数据源。您也许能够访问Items
集合中每个项目的 DataRowView,但我不确定:至于保留数据源,您需要考虑这是否是一个好主意。存储数据源的选项是
Session
或Cache
,但如果结果集相当小,那么当您需要数据源时进行另一次往返可能会更有效。无论您决定做什么,都不要将其存储在 ViewState 中。I'm pretty sure you can only access the datasource that way through the
DataBound
event orItemDataBound
event. You might be able to access the DataRowView for each item in theItems
collection, but I'm not sure:As for persisting the datasource, you need to consider whether that's a good idea. Your options for storing the datasource are
Session
orCache
, 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.