1

I'm writting a simple prototype front end using a GridView that is populated via function, rather than being linked directly to a SqlServer data source.

So I can delete a row/record from grid/underlying database I am currently

  1. Setting the AutoGenerateDeleteButton = true
  2. Displaying the unique record ids in the first column
  3. Handling the RowDeleting event
  4. Obtaining the id by getting the grid.Rows[e.RowIndex].Cells[idIndex].Text
  5. Passing that number through to a function that does the deleting

This seems to be working just fine, but I would rather not display the ids to the users at they don't mean anything to them.

I tried setting the id column's Visible property to false, but this caused step 4 above to return an empty string - and so no record deleted.

So how do I store the hidded id number with each row?

Or am I going about this completely the wrong way?

Follow up to answers:

Thanks for both the answers, ended up going Eric's DataKeyNames way. For other people new to ASP.NET like I am, the steps I used where

  • Between the lines where I set the grids DataSource and called DataBind(), I added

    grid.DataKeyNames = new string[] {"id"};

  • Then in the function handling the RowDeleting I got hold of my id using

    grid.DataKeys[e.RowIndex].Value

2 Answers 2

3

GridView has a DataKeyNames property. When you bind a data source to the grid, you set the DataKeyNames (usually with just one name, your PK field). You don't show the PK, but you can get to it from code-behind.

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

1 Comment

Yep, DataKeys is the way to go.
0

Visible=false means don't render on the page. What you want is either to make it a template field and use a HiddenField to hold the value or set the style on the control to "display: none;". This would be the case if the client side code needed access to the value for an Ajax call or something.

Otherwise use the DataKeyNames property as @Eric Z Beard suggests.

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.