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

Need to sort RadGrid by selection in asp:DropDownList

2 Answers 58 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Seidhkona
Top achievements
Rank 1
Seidhkona asked on 14 Nov 2008, 09:23 PM
Hi guys,

I need to sort my RadGrid by the selected value in the DropDown list. The problem (not counting the fact that i am an absolute newbie in the asp.net/c#/Telerik world and the whole code is the legacy redirected to me from someone who worked on it before me) is that the values which fill the dropdown are not selected from the DataBase, they are dynamically generated in the codebehind... Oh, well, what am I talking about... here is the part of the code with the DropDownList:

<telerik:RadGrid ID="RadGrid1" runat="server" Width="100%" AllowSorting="true" AllowMultiRowSelection="True" 
            AllowPaging="true" PageSize="5" Skin="Telerik" OnItemDataBound="RadGrid1_ItemDataBound" 
            OnNeedDataSource="RadGrid1_NeedDataSource" 
            OnItemCreated="RadGrid1_ItemCreated" onitemcommand="RadGrid1_ItemCommand"
<MasterTableView AutoGenerateColumns="False" CommandItemDisplay="Bottom" AllowSorting="true"
<telerik:GridTemplateColumn HeaderText="Role" UniqueName="Role"
                        <ItemTemplate> 
                            <asp:DropDownList ID="ddl_Roles" CssClass="roles_list" runat="server"
                                <asp:ListItem Text="User" Value="User"></asp:ListItem> 
                                <asp:ListItem Text="Observer" Value="Observer" Enabled="false"></asp:ListItem> 
                                <asp:ListItem Text="Editor" Value="Editor" Enabled="false"></asp:ListItem> 
                                <asp:ListItem Text="Manager" Value="Manager" Enabled="false"></asp:ListItem> 
                                <asp:ListItem Text="Admin" Value="Admin"></asp:ListItem> 
                            </asp:DropDownList> 
                            <div class="button"
                            <asp:Button ID="ChangeRoleButton" CssClass="roles_change" runat="server" OnClientClick="return confirm('Are you sure you want to change this users role?');" CommandName="SaveRole" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID") %>' Text="Save Role" /> 
                            </div> 
                        </ItemTemplate> 
</telerik:GridTemplateColumn> 


and in the codebehind, here is how it is generated:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
        
        if (e.Item is GridDataItem) 
        { 
 
            GridDataItem item = (GridDataItem)e.Item; 
 
            //Access the GridDataItem using the ColumnUniqueName 
            string UserName = item["UserName"].Text; 
            string businessShortName = item["SBN"].Text; 
 
            DropDownList roleListing = (DropDownList)item["Role"].Controls[1]; 
 
            if (Roles.IsUserInRole(UserName, businessShortName + "_admin")) 
            { 
                item["UserName"].Text = roleListing.SelectedValue = "Admin"
            } 
            else if (Roles.IsUserInRole(UserName, businessShortName + "_manager")) 
            { 
                item["UserName"].Text = roleListing.SelectedValue = "Manager"
            } 
            else if (Roles.IsUserInRole(UserName, businessShortName + "_editor")) 
            { 
                item["UserName"].Text = roleListing.SelectedValue = "Editor"
            } 
            else if (Roles.IsUserInRole(UserName, businessShortName + "_observer")) 
            { 
                item["UserName"].Text = roleListing.SelectedValue = "Observer"
            } 
            else 
            { 
                item["UserName"].Text = roleListing.SelectedValue = "User"
            } 




any ideas how to do it? Google search and specific search through Telerik forums didn't help me...

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 17 Nov 2008, 07:57 AM
Hi,

Try the following code snippet in the PreRender event to set the sort expression for the Grid depending on the selectedItems text from the DropDownList.

CS:
 protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items) 
        { 
 
            DropDownList roleListing = (DropDownList)item["Role"].Controls[1]; 
            string selectedItem = roleListing.SelectedItem.Text.ToString(); 
            RadGrid1.MasterTableView.SortExpressions.Clear(); 
            RadGrid1.MasterTableView.SortExpressions.AddSortExpression(selectedItem); 
            break; 
             
        } 
        RadGrid1.MasterTableView.Rebind(); 
    } 


Regards
Shinu.
0
Seidhkona
Top achievements
Rank 1
answered on 17 Nov 2008, 04:09 PM
Thanks for the reply, but when the last statement is called
RadGrid1.MasterTableView.Rebind(); 
i get a Server Error:

Admin is neither a DataColumn nor a DataRelation for table .

So, it seems like this solution doesn't work for me...


Tags
Grid
Asked by
Seidhkona
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Seidhkona
Top achievements
Rank 1
Share this question
or