473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

GridView / EditItemTemplate: using with disconnected DataTable?

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
Jun 27 '08 #1
2 3227
On 11 Apr, 17:21, DC <d...@upsize.dewrote:
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
Jun 27 '08 #2
DC
On 11 Apr., 19:50, Stan <goo...@philphall.me.ukwrote:
On 11 Apr, 17:21, DC <d...@upsize.dewrote:
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
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: sutphinwb | last post by:
Hi - This could be a simple question. When I relate two tables in a datasetet, how do I get that relation to show up in a GridView? The only way I've done it, is to create a separate table in the...
0
by: den 2005 | last post by:
Hi everybody, I created a Gridview with a TemplateField and there is Label control in ItemTemplate and a DropdownList control in EditItemTemplate, I was to displayed them ok when I click the...
4
by: Tomasz Jastrzebski | last post by:
Hello Everyone, I have a GridView control bound to a plain DataTable object. AutoGenerateEditButton is set to true, Edit button gets displayed, and RowEditing event fires as expected.
0
by: landesjoe | last post by:
Hi, here's my problem in short: Text boxes in gridview don't seem to hold their value if the column's .Visible property is changed back and forth. I've got a form with a gridview populated from...
1
by: ist | last post by:
Hi, I am trying to create a Session-Wide, Disconnected, Updatable DataTable (or DataSet) object and use it through an ASP.NET GridView. The scenario is this: 1. When the page is first loaded,...
5
by: =?Utf-8?B?QWRhciBXZXNsZXk=?= | last post by:
Hi All, I have a GridView inside the EditItemTemplate of a FormView. Both FormView and GridView are data bound using an ObjectDataSource. When the FormView's ObjectDataSource object has a...
3
by: nguyenlh | last post by:
code: I have read a example :The use girdview without datasource <asp:GridView AutoGenerateColumns="false" ID="GridView1" runat="server" OnRowCancelingEdit="GridView1_RowCancelingEdit"...
3
by: RobertTheProgrammer | last post by:
Hi folks, I've got another problem. Basically, I'm trying to use a nested GridView, however the nexted GridView displays no values (even though in debug I'm getting valid values into my DataSet. ...
5
by: sandhyascs | last post by:
Hi everyone I’m using the GridView in asp.net and populating the grid dynamically using c#.net.To edit the rows i i m using GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.