I have a grid which lists items in an order. The first column has a GridButtonColumn which a user can click to remove the item from the order.
The data in the grid is loaded using the OnNeedDataSource method, items are bound to the grid and hidden fields are set using OnItemDataBound method, and the event to remove the item uses the OnItemCommand method.
When the user clicks the delete/remove icon in the GridButtonColumn, the page fires the Grid_ItemCommand, but the e.item.DataItem is null, not allowing me to perform my action as I now don't know which item is to be removed from the order?
How do I ensure the DataItem is still available once the user clicks the delete/remove icon?
The data in the grid is loaded using the OnNeedDataSource method, items are bound to the grid and hidden fields are set using OnItemDataBound method, and the event to remove the item uses the OnItemCommand method.
When the user clicks the delete/remove icon in the GridButtonColumn, the page fires the Grid_ItemCommand, but the e.item.DataItem is null, not allowing me to perform my action as I now don't know which item is to be removed from the order?
How do I ensure the DataItem is still available once the user clicks the delete/remove icon?
5 Answers, 1 is accepted
0

Jayesh Goyani
Top achievements
Rank 2
answered on 03 Feb 2012, 01:26 PM
Hello Karl,
Sorry but,You can get "e.item.DataItem" only in ItemDataBound in All controls(asp:gridview,telerik:radTreeList....etc).
In All other event it is always null.
Let me know if any concern.
Note : if you want multiple value in ItemCommand then you can use DataKey.
Thanks,
Jayesh Goyani
Sorry but,You can get "e.item.DataItem" only in ItemDataBound in All controls(asp:gridview,telerik:radTreeList....etc).
In All other event it is always null.
Let me know if any concern.
Note : if you want multiple value in ItemCommand then you can use DataKey.
Thanks,
Jayesh Goyani
0

Karl
Top achievements
Rank 1
answered on 03 Feb 2012, 01:32 PM
Hmm..
So how do I get the item in my grid when I click a control in a row?
Suppose I have 5 items, and I want to remove the 2nd. At the beginning of each row I have a button or image. I click this on the 2nd row which prompts me if I'm sure I want to continue, I click yes and my page posts and I find myself in the ItemCommand method. How do I find out which command was clicked so I know which item to remove?
So how do I get the item in my grid when I click a control in a row?
Suppose I have 5 items, and I want to remove the 2nd. At the beginning of each row I have a button or image. I click this on the 2nd row which prompts me if I'm sure I want to continue, I click yes and my page posts and I find myself in the ItemCommand method. How do I find out which command was clicked so I know which item to remove?
0

Princy
Top achievements
Rank 2
answered on 03 Feb 2012, 01:43 PM
Hello,
I suppose you need to access the item on ItemCommand event. You can check for the CommandName property set for the GridButtonColumn.
C#:
Thanks,
Princy.
I suppose you need to access the item on ItemCommand event. You can check for the CommandName property set for the GridButtonColumn.
C#:
protected
void
RadGrid1_ItemCommand(
object
sender, Telerik.Web.UI.GridCommandEventArgs e)
{
if
(e.CommandName ==
"Delete"
)
{
GridDataItem item = (GridDataItem)e.Item;
//your code
}
}
Thanks,
Princy.
0

Karl
Top achievements
Rank 1
answered on 03 Feb 2012, 01:53 PM
I already have the following code...
protected
void
YourOrderItemsGrid_ItemCommand(
object
sender, GridCommandEventArgs e)
{
try
{
if
(e.CommandName ==
"RemoveFromQuote"
)
{
QuoteLine _item = (QuoteLine)e.Item.DataItem;
if
(_item !=
null
)
{
RemoveItemFromQuote(_item);
}
}
}
catch
(Exception ex)
{
ErrorMessageLabel.Text =
"There was an error removing the item from your order."
;
ErrorMessagePanel.Visible =
true
;
DBTools.LogError(Application, Session,
"AmendFinaliseOrder.aspx"
, Request.QueryString.ToString(),
"YourOrderItemsGrid_ItemCommand()"
, ErrorMessageLabel.Text, ex.Message);
}
}
I've just changed my code so I get the GridDataItem instead, but even this has no useful information in it and I'm still unable to get any of the details with regard to my item in the grid so I can remove it from my collection.
Should my grid be rebound or does this overwrite data and cause conflicts between what was posted and what has been rebound?
0

Jayesh Goyani
Top achievements
Rank 2
answered on 03 Feb 2012, 04:59 PM
Hello,
try with below code snippet.
Let me know if any concern.
Thanks,
Jayesh Goyani
try with below code snippet.
<
MasterTableView
DataKeyNames
=
"ID"
>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"Name"
HeaderText
=
"Name"
UniqueName
=
"Name"
>
</
telerik:GridBoundColumn
>
protected
void
RadGrid1_ItemCommand(
object
sender, GridCommandEventArgs e)
{
if
(e.CommandName ==
"YourCommandName"
)
{
GridDataItem item = e.Item
as
GridDataItem;
// get value from DataKey
string
str1 = item.GetDataKeyValue(
"ID"
).ToString();
// get value from Column
string
str2 = item[
"Name"
].Text;
}
}
Let me know if any concern.
Thanks,
Jayesh Goyani