I have a webgrid where I would like to add a column where each cell contains a name and an ID, with the name displayed in the cell. I can do this by creating my own class as below, and make each cell element an instance of this class:
private class NameAndID { private string name; private int ID; public NameAndID(string name, int ID) { this.name = name; this.ID = ID; } public override string ToString() { return this.name; } public int getID() { return this.ID; } }
This works server-side because I can use the getID method to retrieve the ID of an element, but how do I get the ID at client-side with javascript? The cell.getValue() javascript method only returns the name! And is this the way to go or can I create a custom column that contains a name and an ID in some other way?
EDIT: I have tried making a hidden column that contains the ID but this doesn't work, because a hidden column is not sorted when the grid is sorted, and therefore the proper IDs are not linked to the proper names after a sort, so I would like to avoid hidden columns!
I see now, thanks for the clarification. If the hidden ID column is not being sorted along with its corresponding row, as you've described here, then I don't believe that this is functioning correctly.
Whether this is a bug or not may depend on how you're putting the ID of each row into the grid in the first place. If you're basing this off the row's Index, and if you're recreating the rows on every postback, then this is an expected result - the index of the row matches its visual position, and so is changed when you sort.
Additional related questions:
Can you reproduce this behavior in a concise sample application that we can run and debug? If so, then you should submit a support request so that a Developer Support Engineer can investigate this, to better determine if this is a bug - to get it fixed if it is, and to see if we can find a workaround whether it is or isn't a bug. If this turns out to be a likely bug, we'll need you to submit this as a support request anyway.
I'll try to explain what I want to accomplish, because as I understand it, I've already tried your proposed solution: My objects have two properties: ID and Name. In my unbound webgrid, each row should contain one object. I would like the name of the object to be displayed but not the ID (e.g. the ID must be hidden). I have already tried making an column that contains the ID and made this column hidden, and made a name-column which is not hidden. The thing is that, when I sort the grid client-side by clicking the header of the name-column, the ID-column is not sorted. This means that the ID of the row doesn't "follow" the name, and that's no good!
I'll try making an example:
Row 1: ID = 1, Name = "Carlos"
Row 2: ID = 2, Name = "Andrew"
When I click the name-column and sort the names ascending I get:
Row 1: ID = 1, Name = "Andrew"
Row 2: ID = 2, Name = "Carlos"
The ID-name combinations are not right anymore! Therefore I am seeking a solution where the name-column contains the name and the ID (hidden like a key), so the IDs and the names stick to each other. Hope this explains my problem. Can it be done? :)
The Key of a cell should match the Key of its corresponding column. I wouldn't expect that setting the Key of the cell to something different to the Key of its column to take any effect, but I'm not 100% certain whether it should or shouldn't.
I'm also not certain I understand the end result that the original poster is after.
What I do know is that WebGrid won't handle multiple properties on a single object in a single column in this fashion. It'll display the result of calling ToString() on an instance of the object. If using a dropdown won't work, another approach may be to add unbound columns to the grid, one to represent each property of the object you want to display; you can put the values into the grid cells using the InitializeRow event, and you can update the underlying objects in the UpdateRow or UpdateRowBatch events.
Anyone from Infragistics who can confirm/deny that such an option exists?
I coudn't find exactly how to access key client side but there has to be a way to do that as for UltraGridRow there is a method called "getCellFromKey" so it has to be accessible in javascript. just need to find out how.