datagrid created from a view

  • Thread starter Thread starter David
  • Start date Start date
D

David

Hello,

I have a datagrid populated with rows from a view. Because they are from a
view, the rows are not actual database records, and therefore lack unique
identifiers.

So I have a command button for each row. When the button is pressed, I want
to go to another page with all the information in that selected row.
Normally I would do this (if the rows are actual records, that is)

//take the datakey value
string selected_id = dgMyDataGrid.DataKeys[e.Item.ItemIndex]

//and do this before tranfering to another page.
Context.Items.Add("uniquekey", selected_id);
Then on the second page, I would take the selected_id and retreive the
record from the database (I've always wondered if that trip to the database
server would be necessary).

But I can't even do this with rows from a view because there is no unique
identifier. But I still want to take the row from the view and give it to
the destination page.
 
Couldn't you just put the data from the selected grid row into an object
and store that in the context items collection? (Define your own class or
structure to hold the data, then create an instance of it and place it in
the items.)

Then you could pull the object out on page2 and you would have all the data
you need (rather than just pulling the key out of the context items).

Hope this helps,
bliz
--
Jim Blizzard
Sr .NET Developer Evangelist
Microsoft

Your Potential. Our Passion.

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only, so that others may benefit. Thanks.


--------------------
 
The datagrid's datasource is a collection object.

dgMyDataGrid.DataSource = myCollectionObject.

But I would not know how to make recognize the selected single object within
the collection when the user selects the row. How would I do that in the
web environment. I know that is easy in windows forms.
 
But based on the datagrid.SelectedItem.Cells collection you can get the
cells for the selected row in your datagrid during the postback.

Add the data from each of the cells to the instance custom class or
structure, then add that to the cache.

See the "MyDataGrid_Select" method in the example at :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemWebUIWebControlsDataGridClassSelectedIndexTopic.asp.

Note: if you want to run the VB version that sample code you'll have to
change the line
"Dim Cart As DataTable"
to
"Dim Cart As New DataTable()"

Slight bug in the docs.... :)

Hope this helps,
bliz

--
Jim Blizzard
Sr .NET Developer Evangelist
Microsoft

Your Potential. Our Passion.

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only, so that others may benefit. Thanks.


--------------------
 
Back
Top