GridView / EditItemTemplate: using with disconnected DataTable?

GridView / EditItemTemplate: using with disconnected DataTable?

am 11.04.2008 18:21:09 von DC

Hi,

I am using a GridView to present data in a DataTable, which I store
only in ViewState and when the user hits the "OK" button the rows in
the DataTable will be used to execute transactions.

I need the ability to insert new rows (which is easily done with
DataTable.Rows.Add(newRow) and rebinding) and then present a row with
different TemplateFields for those rows (I need multiple rows in this
insert state).

I could not get this to work with an CommandField, I think this was
not designed to be able to edit underlying DataTable objects?

I thought that the Insert/EditItemTemplates would be chosen by the
state of the row. I need something to convince the GridView to display
a number of rows that I specify in code behind with the
EditItemTemplate.

I also tried to selectively hide and show certain controls only using
the ItemTemplate and I think this is possible - just with way more
effort.

TIA for any hints.
Regards
DC

Re: GridView / EditItemTemplate: using with disconnected DataTable?

am 11.04.2008 19:50:56 von Stan

On 11 Apr, 17:21, DC wrote:
> I need the ability to insert new rows (which is easily done with
> DataTable.Rows.Add(newRow) and rebinding) and then present a row with
> different TemplateFields for those rows (I need multiple rows in this
> insert state).

The GridView control does not have an "insert" state. If you are
prepared to upgrade to VS2008 then there is a ListView control which
does so but even then only one row at a time.

> I could not get this to work with an CommandField, I think this was
> not designed to be able to edit underlying DataTable objects?

It is the DataSource assigned to the GridView that carries out
updating and inserting but it needs a unique key (passed as a
parameter to the Update query) to identify the row in the underlying
table. It has to be configured to take this from the DataKey value(s)
of the GridView control. A command button with a CommandName property
set to "Update" will do this provided that the DataKeyNames property
of the GridView control is set up properly.

> I thought that the Insert/EditItemTemplates would be chosen by the
> state of the row. I need something to convince the GridView to display
> a number of rows that I specify in code behind with the
> EditItemTemplate.

The row which is rendered using the EditItemTemplate is the one with
an index corresponding to the EditItemIndex property of the GridView
control (note that indexes refer to the index of the row relative to
the currently visible page of the grid not the table as a whole -
unless PageMode is disabled).

Normally a command button (e.g. a LinkButton) in the ItemTemplate with
a CommandName property set to "Edit" will (when clicked) set the
EditItemIndex property to the index of the row containing it, thus
placing that row in the Edit state. (An EditItemIndex value of -1 will
mean that no row is in the Edit state).

The implication of this is that only one row can be in the Edit state
at any one time.

> I also tried to selectively hide and show certain controls only using
> the ItemTemplate and I think this is possible - just with way more
> effort.

The appearance of individual rows in the "read only" state can be
varied in one of two ways:

(1) Binding certain properties of controls contained in the
ItemTemplate to an expression derived from column values of the data
(involving Eval() function as part of it). For example if it is
possible to construct an expression that returns a boolean value, then
that can be bound to the visible property of such a control hence
showing or hiding it depending on the data values for that row.

(2) Write some code and place it in the GridView's RowDataBound event
handler. There you can access the controls using the event argument
e.Row property and the FindControl() method (but make it conditional
on e.Row.RowType == DataControlRowType.DataRow) This alternative will
allow other more complex scenarios that might not be achievable with a
single data dependent expression.

Hope that's useful.

>
> TIA for any hints.
> Regards
> DC

Re: GridView / EditItemTemplate: using with disconnected DataTable?

am 11.04.2008 23:21:05 von DC

On 11 Apr., 19:50, Stan wrote:
> On 11 Apr, 17:21, DC wrote:
>
> > I need the ability to insert new rows (which is easily done with
> > DataTable.Rows.Add(newRow) and rebinding) and then present a row with
> > different TemplateFields for those rows (I need multiple rows in this
> > insert state).
>
> The GridView control does not have an "insert" state. If you are
> prepared to upgrade to VS2008 then there is a ListView control which
> does so but even then only one row at a time.
>
> > I could not get this to work with an CommandField, I think this was
> > not designed to be able to edit underlying DataTable objects?
>
> It is the DataSource assigned to the GridView that carries out
> updating and inserting but it needs a unique key (passed as a
> parameter to the Update query) to identify the row in the underlying
> table. It has to be configured to take this from the DataKey value(s)
> of the GridView control. A command button with a CommandName property
> set to "Update" will do this provided that the DataKeyNames property
> of the GridView control is set up properly.
>
> > I thought that the Insert/EditItemTemplates would be chosen by the
> > state of the row. I need something to convince the GridView to display
> > a number of rows that I specify in code behind with the
> > EditItemTemplate.
>
> The row which is rendered using the EditItemTemplate is the one with
> an index corresponding to the EditItemIndex property of the GridView
> control (note that indexes refer to the index of the row relative to
> the currently visible page of the grid not the table as a whole -
> unless PageMode is disabled).
>
> Normally a command button (e.g. a LinkButton) in the ItemTemplate with
> a CommandName property set to "Edit" will (when clicked) set the
> EditItemIndex property to the index of the row containing it, thus
> placing that row in the Edit state. (An EditItemIndex value of -1 will
> mean that no row is in the Edit state).
>
> The implication of this is that only one row can be in the Edit state
> at any one time.
>
> > I also tried to selectively hide and show certain controls only using
> > the ItemTemplate and I think this is possible - just with way more
> > effort.
>
> The appearance of individual rows in the "read only" state can be
> varied in one of two ways:
>
> (1) Binding certain properties of controls contained in the
> ItemTemplate to an expression derived from column values of the data
> (involving Eval() function as part of it). For example if it is
> possible to construct an expression that returns a boolean value, then
> that can be bound to the visible property of such a control hence
> showing or hiding it depending on the data values for that row.
>
> (2) Write some code and place it in the GridView's RowDataBound event
> handler. There you can access the controls using the event argument
> e.Row property and the FindControl() method (but make it conditional
> on e.Row.RowType == DataControlRowType.DataRow) This alternative will
> allow other more complex scenarios that might not be achievable with a
> single data dependent expression.
>
> Hope that's useful.
>
>
>
>
>
> > TIA for any hints.
> > Regards
> > DC- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

Hi Stan,

> Hope that's useful.

are you kidding? You just wrote the EditItemTemplate bible! Thanks a
lot, I think I will use the (1) which is simple and probably feasible
for me.

Best regards
DC