O
Oleg Ogurok
Hi there,
I'm looking for a way to add a row to DataGrid without binding the data
source to it.
In the DataGrid code, when DataBind() is called, ViewState is discarded and
new set of controls is created with the values from the datasource.
On a postback, DataGrid recreates the controls based on the values in
ViewState["_!DataSourceItemCount"] and ViewState["_!ItemCount"].
Let's say I have a button in a Header of one of the columns. The button has
"AddNewRow" as its CommandName. I can then override OnItemCommand, catch the
desired command and increment the _!DataSourceItemCount and _!ItemCount
values in ViewState, as follows:
protected override void OnItemCommand(DataGridCommandEventArgs e)
{
base.OnItemCommand (e);
if (e.CommandName == "AddNewRow")
{
int itemCount = (int)ViewState["_!ItemCount"];
ViewState["_!ItemCount"] = itemCount + 1;
int dataSourceItemCount = (int)ViewState["_!DataSourceItemCount"];
ViewState["_!DataSourceItemCount"] = dataSourceItemCount + 1;
CreateChildControls();
}
}
The probem is that even though I'm calling CraeteChaildControls() to
recreate controls, the controls come out empty. This is because ViewState of
those controls has already been restored. I'm wondering if there is a way to
restore the ViewState for those controls once again.
Thanks.
-Oleg
I'm looking for a way to add a row to DataGrid without binding the data
source to it.
In the DataGrid code, when DataBind() is called, ViewState is discarded and
new set of controls is created with the values from the datasource.
On a postback, DataGrid recreates the controls based on the values in
ViewState["_!DataSourceItemCount"] and ViewState["_!ItemCount"].
Let's say I have a button in a Header of one of the columns. The button has
"AddNewRow" as its CommandName. I can then override OnItemCommand, catch the
desired command and increment the _!DataSourceItemCount and _!ItemCount
values in ViewState, as follows:
protected override void OnItemCommand(DataGridCommandEventArgs e)
{
base.OnItemCommand (e);
if (e.CommandName == "AddNewRow")
{
int itemCount = (int)ViewState["_!ItemCount"];
ViewState["_!ItemCount"] = itemCount + 1;
int dataSourceItemCount = (int)ViewState["_!DataSourceItemCount"];
ViewState["_!DataSourceItemCount"] = dataSourceItemCount + 1;
CreateChildControls();
}
}
The probem is that even though I'm calling CraeteChaildControls() to
recreate controls, the controls come out empty. This is because ViewState of
those controls has already been restored. I'm wondering if there is a way to
restore the ViewState for those controls once again.
Thanks.
-Oleg