Question

let's say i have an order and order details.
the view will contains the order fields, and a Telerik Grid for the details
i always maintain a reference of the Order in the session.

Session["Order"] = order;

and when the user add an order detail to the grid, I'm saving it in the Order reference.

public ActionResult Grid_AddDetail(OrderDetail orderDetail)  {
(Session["order"] as Order).Details.Add(orderDetail);    
}  

the problem is when i need to update the row, how can i determine which detail in Order Details has been updated?

public ActionResult Grid_UpdateDetail(OrderDetail orderDetail)  {
///how will i compare the element in the details, with the orderDetail?        
(Session["order"] as Order).Details.IndexOf(orderDetail) = orderDetail;
}  

the problem can be solved by adding a serial number column, and compare the incoming detail with the existed on in my reference, by overriding the Equal:

public overrid Equal(object obj){
return (obj as OrderDetail).Serial == this.Serial;
}

but i want the serial number column to be invisible, but if i do so, it will not be presented in the incomming detail.

Was it helpful?

Solution 2

what I did is:
added a column called Serial
made the column width set to 0.

columns.Bound(m => m.Serial).Title("").Sortable(false).Width(0);

and it will be presented in (insert, update) but the problem in delete is to make him (as Brett said) as a Datakey.

public ActionResult Grid_AddDetail(OrderDetail orderDetail)  {
  if ((Session["order"] as Order).Details.Count != 0)
     item.Serial= (Session["order"] as Order).Details.Max(d => d.Serial) + 1;
  (Session["order"] as Order).Details.Add(orderDetail);    
 } 

public ActionResult Grid_UpdateDetail(OrderDetail orderDetail)  {
///order detail now contains the serial number.  
(Session["order"] as Order).Details.IndexOf(orderDetail) = orderDetail;
} 

OTHER TIPS

If you just want to make the column invisible, I think this should help:

AutoGenerateColumns="false"

That will force you to generate the columns displaying the information, rather than the gridview automatically creating them for you. So now you will need to do something like this to get the order to display

<asp:TemplateField>
      <ItemTemplate>
           <b><%# DataBinder.Eval(Container.DataItem, "Order") %>:</b>
      </ItemTemplate>
</asp:TemplateField>

EDIT:

To access the Serial Number when it is not visible, you will need to use DataKeys:

orderDetail.DataKeyNames = new string[] { "Serial" };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top