SqlDataSource CurrentItem

G

Guest

I have a SqlDataSource bound to DataView control. When the selected index of
the DataView changes I would like to get the value of a field (column) from
the bound Row. The column's visible property is false. I thought I would be
able to handle the selected event of the SqlDataSource and get the Row data,
but there is nothing there.

Where are the SqlDataSourceView items? The DataView's selected item doesn't
have a DataItem, so I can't access the Row. If I show the column I want to
hide, I can access the "Text" of the GridView column, but the text has been
HtmlEncoded. (and either way of course I don't want it in the grid).

Where is the CurrentItem and can it be accessed?
 
G

Guest

You might add the DataKeyNames to the declaration of the GridView then while
handling the SelectedindexChanged you would get that key, which you can use
to query the database programmatically, e.g.
string strKey = ((GridView)sender).SelectedDataKey.Value.ToString();
 
G

Guest

Thanks for the reponse. I can't believe that I would have to call back to the
database again, but after messing with this for some time, that really does
seem to be the emerging solution.
 
G

Guest

I think that the SelectedIndexChanged does not rebind the Grid; it relies on
the ViewState to change the selectedRow; therefore you do not get the
DataItem associated with the SelectedRow in this event. To get a reference
to the DataItem you would have to handle some GridViewRow event such as the
RowDataBound event.

As for going back to the server to get the details of a selected item, I
would consider one of the following approaches: 1) add the value to the
DataKeyNames; that way you can retrieve uncorrupted, 2) use declarative
syntax to retrieve the details of a selected row in a detailsView instead of
programming further code based on the selectedIndex.

http://66.129.71.130/QuickStartv20/...aspnet/samples/data/GridViewMasterDetails.src
 
G

Guest

Hey, Phillip,

You're right. I was looking for ItemDataBound and kept passing over
RowDataBound. This problem was complicated by the fact I was not checking to
make sure the current row was a DataRow.

e.Row.RowType == DataControlRowType.DataRow

The DataItem is null on the Header and Footer rows.

Thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top