views:

246

answers:

2

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.

A: 

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" };
Brett
is <asp:TemplateField> available in asp.net mvc ?and as I know, Telerik Grid has the ability to define a template for each column. and by the way, I'm using Ajax Binding, and AutoGenerateColumns is false.
Nour Sabouny
@Nour Sabouny - I'm actually not too familiar with MVC, but I do use the Telerik Grid. I was thinking that turning AutoGenerateColumns ="false" would solve your problem of not seeing the serial column (but maybe cause you not to be able to find it.) To fix this, you would use DataKeys, set "serial" as a datakey, which makes it accessible even if it is not showing. Let me know if that helps at all
Brett
A: 

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;
} 
Nour Sabouny