1

I have SqlDataSource assigned to a GridView, and a lot of the fields returned are only used in the code behind to determine whether a column is visible, etc. Currently I bind these vlaues to a hiddenfield, however this adds a lot of code to the source file which isn't ideal.

Can I get the values for the row from the codebehind, without having to assign them to a hiddenfield in the markup?

1
  • I don't see why you couldn't - perhaps handle the one of the DataBinding or Creating events for the GridView, and pull the information you need from the SqlDataSource? If I had more time, I'd try and put together a sample to verify it, but it seems like a plausible approach. Commented Aug 25, 2011 at 10:07

1 Answer 1

3

In your code behind, you can access the values through the DataItem object

Example

DataRowView rowView = (DataRowView)e.Row.DataItem;

// Retrieve the state value for the current row. 

String state = rowView["state"].ToString();

Or you could convert the value to an object and play around with it.

Take a look here for more info http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.dataitem.aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.