This is my first experience using the UltraWebGrid. I'm trying to delete a row from a grid using the row's delete button. The grid is bound to a List collection. Had trouble from the start. I tried to delete server side in the button click event (using Rows.removeAt(index) ) but kept getting this error:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
I could debug and see the collection had the right number of items. Determined that this was related to doing a databind in the OnInit() event. That was needed to get around this error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
which I got because I need to do this:
<CellTemplate> <asp:Label ID="lblName" runat="server" Text='<%# Eval("Type.Name") %>' /></CellTemplate>
I was able to get the delete to work in the CellButtonClick (I have multiple buttons) server side by deleting from the DB and refreshing:
int reqID = Convert.ToInt32(e.Cell.Row.Cells.FromKey("ID").Value);
if (e.Cell.Column == uwgRequests.Columns.FromKey("Delete")){ ServReqManager.DeleteRequest(reqID); ServReqManager.CommitChanges(); RefreshGrid();}
Things were OK. My problem now is I need to confirm the delete using javascript. Ideally I could just toss up a confirm box and then continue on to the CellButtonClick event as before. Is there a way to do that I haven't seen in any examples.
Alternatively I considered trying to delete client side as suggest by many posts in the forums. However I get the same 'Index was out of range' error as above.
function confirm_delete(gridName, cellId) { var cell = igtbl_getCellById(cellId); if (cell.Column.Key == "Delete") { if (confirm("Are you sure you want to delete this Service Request?")) { var grid = igtbl_getGridById(gridName); grid.AllowDelete = 1; var row = cell.getRow(); row.deleteRow(); return true; } else return false;
Any tips or suggestions?
Hi,
I think that the reason for that error could be the way you deal whit the number of rows. Maybe you have 20 rows, but it doesn't mean that calling row(20) is row 20, because it starts at 0 and ends at 19.
Try to do a loop just to display the contents of one cell, counting from 0 to (NumberOfRows - 1) and if the loop displays ok, the you should check you code