This is a migrated thread and some comments may be shown as answers.

Help Needed when sorting Grid

2 Answers 115 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Simon
Top achievements
Rank 1
Simon asked on 20 Jan 2012, 05:54 PM
Dear all Guru's
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>
And the code behind wired up as follows:
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);
               }
           }
            
       }
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

2 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 20 Jan 2012, 06:19 PM
Hello,

<telerik:GridButtonColumn Text="Select" CommandName="MySelect" ButtonType="LinkButton"
                       UniqueName="column">
                   </telerik:GridButtonColumn>
                   <telerik:GridButtonColumn Text="Edit" CommandName="MyEditCustomer" ButtonType="LinkButton"
                       UniqueName="EditCustomer">
                   </telerik:GridButtonColumn>
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "MySelect")
            {
                GridDataItem item = e.Item as GridDataItem;
                string id = item.GetDataKeyValue("CustomerId").ToString();
                var destination = string.Format("CustomerDetail.aspx?CustomerId={0}", id);
                Response.Redirect(destination);
            }
            if (e.CommandName == "MyEditCustomer")
            {
                GridDataItem item = e.Item as GridDataItem;
                string id = item.GetDataKeyValue("CustomerId").ToString();
                var destination = string.Format("CustomerEdit.aspx?CustomerId={0}", id);
                Response.Redirect(destination);
            }
        }


or

<telerik:GridBoundColumn SortExpression="ID" DataField="ID"


Let me know if any concern.

Thanks,
Jayesh Goyani
0
Simon
Top achievements
Rank 1
answered on 23 Jan 2012, 11:36 AM
Great thanks.  Work like a charm..
Tags
Grid
Asked by
Simon
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Simon
Top achievements
Rank 1
Share this question
or