Ok, cool that it's a derived class.
But, ViewState is loaded between the OnInit and OnLoad events in the page.
That's also about when the posted GridView rebuilds itself from the ViewState.
realize that). I suppose you could call CreateChildControls() on yourself
> I'm getting ViewState["_!ItemCount"] inside a class derived from
> DataGrid (using C# inheritance), so I'm getting the right value. I can
> increment this value, and DataGrid will recognize it when rebuilding
> controls. However, I can't seem to figure out how to make the controls
> restore their ViewState values.
>
> I know it's possible to add a DataRow to a DataTable and call
> DataBind() again but I'm trying to avoid that.
>
> -Oleg.
>
> "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
> news:1016789632581414246954880@msnews.microsoft.com...
>
>> That won't work because each control has it's own bucket of ViewState
>> -- the keys aren't shared across the page. IOW, you indexing into
>>
> ViewState["ItemCount"]
>
>> will be a different entry in ViewState than the DataGrid's
>>
> ViewState["ItemCount"].
>
>> Why don't you just use the DataGrid object and create a new row?
>>
>> -Brock
>> DevelopMentor
>>
http://staff.develop.com/ballen >>> 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