Plato (DevExpress Support)
05.14.2009
1 Solution
Indeed, in this case, the ASPxGridView won't be able to work properly. This is because the ASPxGridView does not cache data, so you must provide it to the control at the every request to the server. Here is the code which must be used when setting the control's DataSource at runtime:
[C#]protected void Page_Init(object sender, EventArgs e) { // initialize SomeDataTable ASPxGridView1.DataSource = SomeDataTable; ASPxGridView1.DataBind(); } protected void Page_Load(object sender, EventArgs e) { ... }
UPDATE:
With the newest version ASPxGridView supports the caching mechanism (it is enabled by default). If you wish to use it and bind the grid at runtime, use the approach posted in the ‘Bind a grid to a DataTable via code’ example. In this case, ASPxGridView will be re-populated only when it requires data. In other cases the grid will take data from its own custom callback state. You can learn more about this mechanism here:
Here is some code snippet:
[C#]protected void Page_Load(object sender, EventArgs e) { //Bind the grid only once if (!IsPostBack) grid.DataBind(); } protected void grid_DataBinding(object sender, EventArgs e) { // Assign the data source in grid_DataBinding grid.DataSource = GetTable(); }
-
protected dataset binddata()
{
your sourcecode here and
bind grid
then return dataset
}
protected void page_load()
{
if(!ispostback)
{
binddata();
}
} -
What difference it will make, if the code inside page_init is in page_load without IsPostback check
-
If ASPxGridView does not have a data source when it is required, the control behavior is unexpected. For example, if the Row Cache is disabled, ASPxGridView requests data before the Load event is raised. If you assign a data source at runtime in the page Load event handler, this can cause an issue.
-
I think loading the data every postback is expensive. Just load the data during the PageIndexChanged or BeforeColumnSortingGrouping, or ProcessColumnAutoFilter.
-
I am not sure that this solution is universal. However, if it operates as expected in your scenario, I believe that it can be used as well.