Dear all Guru's
I have a grid as below
And the code behind wired up as follows:
When I sort the grid on the header I get the following error.
Unable to cast object of type 'Telerik.Web.UI.GridHeaderItem' to type 'Telerik.Web.UI.GridDataItem'.
Obviously if I wrap the ItemCommand in a if(!IsPostback) it doens't error but then I lose the functionality of my command buttons in the Grid.
Can someone tell what I have missed here?
Many thanks for any help
I have a grid as below
<telerik:RadGrid HeaderStyle-Wrap="false" ItemStyle-Wrap="true" ID="gvCustomers" runat="server" AutoGenerateColumns="False" GridLines="None" AllowSorting="true" OnNeedDataSource="gvCustomers_NeedDataSource" OnItemCommand="gvCustomers_ItemCommand"> <MasterTableView DataKeyNames="CustomerId"> <CommandItemSettings ExportToPdfText="Export to Pdf"></CommandItemSettings> <RowIndicatorColumn> <HeaderStyle Width="20px"></HeaderStyle> </RowIndicatorColumn> <ExpandCollapseColumn> <HeaderStyle Width="20px"></HeaderStyle> </ExpandCollapseColumn> <Columns> <telerik:GridBoundColumn DataField="Title" HeaderText="Title" UniqueName="Title"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="FName" HeaderText="First Name" UniqueName="FName"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="LName" HeaderText="Last Name" UniqueName="LName"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="CompanyName" HeaderText="Company" UniqueName="CompanyName"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="Address1" HeaderText="Address 1" UniqueName="Address1"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="Town" HeaderText="Town" UniqueName="Town"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="Postcode" HeaderText="Postcode" UniqueName="Postcode"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="Telephone" HeaderText="Tel" UniqueName="Telephone"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="Mobile" HeaderText="Mobile" UniqueName="Mobile"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="Email" HeaderText="Email" UniqueName="Email"> </telerik:GridBoundColumn> <telerik:GridButtonColumn Text="Select" CommandName="Select" ButtonType="LinkButton" UniqueName="column"> </telerik:GridButtonColumn> <telerik:GridButtonColumn Text="Edit" CommandName="EditCustomer" ButtonType="LinkButton" UniqueName="EditCustomer"> </telerik:GridButtonColumn> </Columns> </MasterTableView> </telerik:RadGrid>protected void gvCustomers_ItemCommand(object sender, GridCommandEventArgs e) { if (!IsPostBack) { var item = (GridDataItem) e.Item; string id = item.GetDataKeyValue("CustomerId").ToString(); if ((e.Item is GridDataItem) && (e.CommandName == "EditCustomer")) { var destination = string.Format("CustomerEdit.aspx?CustomerId={0}", id); Response.Redirect(destination); } if ((e.Item is GridDataItem) && (e.CommandName == "Select")) { var destination = string.Format("CustomerDetail.aspx?CustomerId={0}", id); Response.Redirect(destination); } } }Unable to cast object of type 'Telerik.Web.UI.GridHeaderItem' to type 'Telerik.Web.UI.GridDataItem'.
Obviously if I wrap the ItemCommand in a if(!IsPostback) it doens't error but then I lose the functionality of my command buttons in the Grid.
Can someone tell what I have missed here?
Many thanks for any help