Gridview: How do I get the datafield of a boundcolumn in codebehind
-
Hi, I have a user control with a gridview control. In the RowUpdating event I raise an event caught in the page to do the update to the database. I want to get the datafield of each cell to know which field in the database to update. Another question related to this is that I've noticed that if I make AutogenerateColumns = true, the columns are not created in the same order than it is specified in the select query returned from the database. How can I make sure that the columns is bound in the same order as it is returned from the db?
-
Hi, I have a user control with a gridview control. In the RowUpdating event I raise an event caught in the page to do the update to the database. I want to get the datafield of each cell to know which field in the database to update. Another question related to this is that I've noticed that if I make AutogenerateColumns = true, the columns are not created in the same order than it is specified in the select query returned from the database. How can I make sure that the columns is bound in the same order as it is returned from the db?
-
- Populate your datakey names. 2) Now you can access them in side RowUpdating by gridView1.DataKeyNames[e.RowIndex].Values[columnName] 1) Try adding a group by clause int he select query
Thanks Laddie Kindly rate if the answer was helpful
I don't think I explained my problem clearly. First I want to know how to find the name of the datafield which the cell is bound to in each cell in a row, for ex. If the value in column[0] is "1234567" i want to get from code that the datafield is IDNumber, to be able to know which field in the db to update. The reason why I don't have this info available is because the grid is generic in a user control and I bind the grid to a list. Secondly I want to know how to specify the order of the columns returned and not the rows of data. I always thought it will databound the columns in the same order as the select query: for instance: Select IDNumber, suname, initials from Employee. When I bind the datasource: the first column is Initials, sencond column is surname and the last column is IDNumber. I don't understand why this happens, any idea? Thanx in advance