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

Sorting and Filter text box size not working

1 Answer 56 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Felice
Top achievements
Rank 1
Felice asked on 27 Sep 2014, 02:47 PM
I am unable to get sorting  to work for this grid as well as to control the size of the filter text boxes for this grid.
I think the problem is associated with the way I select the datasource but I do not know how to fix it and I need to allow selecting the Sql table to the user.

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = SqlDataSource2;
            DropDownList1.DataBind();
        }
        SetDataSource();
    }
 
    protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedIndex > 0)
        {
            SetDataSource();
            RadGrid1.Rebind();
        }
    }
 
    private void SetDataSource()
    {
        if (DropDownList1.SelectedIndex > 0)
        {
            string tableInUse = DropDownList1.SelectedItem.Text;
            RadGrid1.DataSource = SqlDataSource1;
            SqlDataSource1.DeleteCommand = "DELETE FROM [" + tableInUse + "] WHERE [Id] = @Id";
            SqlDataSource1.InsertCommand = "INSERT INTO [" + tableInUse + "] ([Code], [Description], [Bkgroup], [Stgroup], [Quantity], [BomNote], [UnitEng], [AlternCost]) VALUES (@Code, @Description, @Bkgroup, @Stgroup, @Quantity, @BomNote, @UnitEng, @AlternCost)";
           SqlDataSource1.SelectCommand = "SELECT prod.Id, prod.Code, prod.Description, prod.Bkgroup, prod.Stgroup, prod.Quantity," +
                                           "prod.BomNote, prod.UnitEng, prod.AlternCost, comp.Unit, comp.Cost, comp.NoteComp " +
                                           "FROM  " + tableInUse + " prod " +
                                           "left JOIN  Components comp ON  comp.Code = prod.Code";
            SqlDataSource1.UpdateCommand = "UPDATE [" + tableInUse + "] SET [Code] = @Code, [Description] = @Description, [Bkgroup] = @Bkgroup, [Stgroup] = @Stgroup, [Quantity] = @Quantity, [BomNote] = @BomNote, [UnitEng] = @UnitEng, [AlternCost] = @AlternCost WHERE [Id] = @Id";
        }
    }
 
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = SqlDataSource1;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        RadGrid1.MasterTableView.TableLayout = GridTableLayout.Auto;
        RadGrid1.AllowFilteringByColumn = true;
        RadGrid1.MasterTableView.AllowFilteringByColumn = true;
        RadGrid1.Rebind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        RadGrid1.MasterTableView.TableLayout = GridTableLayout.Auto;
        RadGrid1.AllowFilteringByColumn = false;
        RadGrid1.MasterTableView.AllowFilteringByColumn = false;
        RadGrid1.Rebind();
    }
      protected void RadGrid1_ItemCreated1(object sender, GridItemEventArgs e)
       {
       if (e.Item is GridFilteringItem)
        {
            GridFilteringItem filteringItem = e.Item as GridFilteringItem;
            //set dimensions for the filter textbox  
            TextBox box = filteringItem["Code"].Controls[0] as TextBox;
            box.Width = Unit.Pixel(30);
        }
    }

If I try to use the Item_created event to control the size of the filter text boxes, I get a null reference exception.

<div>
           <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" AppendDataBoundItems="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged1" DataTextField="Name" DataValueField="Name">
               <asp:ListItem Text="-Select a BOM" />
           </asp:DropDownList>
           <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:BOMConnectionString %>" SelectCommand="SELECT [Name] FROM [IndexTabb]"></asp:SqlDataSource>
           <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Filter On" />
           <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Filter Off" />
           <br />
           <br />
           <br />
           <telerik:RadGrid ID="RadGrid1" runat="server" Culture="it-IT" AllowPaging="True" AllowSorting="True" AllowAutomaticDeletes="True" AllowAutomaticInserts="True" AllowAutomaticUpdates="True" AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemCreated="RadGrid1_ItemCreated1">
               <ExportSettings ExportOnlyData="True">
                   <Pdf PageWidth="">
                   </Pdf>
               </ExportSettings>
               <ClientSettings>
                   <Scrolling AllowScroll="True" UseStaticHeaders="True" />
               </ClientSettings>
               <MasterTableView CommandItemDisplay="Top" DataKeyNames="Code" InsertItemPageIndexAction="ShowItemOnCurrentPage" AllowNaturalSort="False">
                   <CommandItemSettings ShowExportToExcelButton="True" />
                   <Columns>
                       <telerik:GridEditCommandColumn ButtonType="ImageButton">
                           <HeaderStyle Width="30px" />
                       </telerik:GridEditCommandColumn>
                       <telerik:GridButtonColumn ButtonType="ImageButton" Text="Delete" CommandName="Delete" FilterControlAltText="Filter column1 column" ConfirmDialogType="RadWindow" ConfirmText="Do you really want to delete this project and all its content?" UniqueName="Cancel">
                           <HeaderStyle Width="30px" />
                       </telerik:GridButtonColumn>
                   </Columns>
                   <EditFormSettings>
                       <EditColumn UniqueName="EditCommandColumn1" FilterControlAltText="Filter EditCommandColumn1 column"></EditColumn>
                   </EditFormSettings>
                   <PagerStyle AlwaysVisible="True" />
               </MasterTableView>
               <PagerStyle AlwaysVisible="True" />
           </telerik:RadGrid>
           <br />
           <br />
       </div>
       <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:BOMConnectionString %>">
       </asp:SqlDataSource>
And sorting is not working. When I click on the header of the column I can see the small arrow changing up or down but nothing happens.
How can I get control of the filter text boxes size and make the sorting working in this context?
Any help will be appreciated.

1 Answer, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 02 Oct 2014, 08:12 AM
Hi Felice,

To achieve this binding scenario, please use a combination of the two approaches presented in these articles:
http://www.telerik.com/help/aspnet-ajax/grid-advanced-data-binding.html
http://www.telerik.com/help/aspnet-ajax/grid-changing-structure-dynamically.html

For your convenience, I'm also sending a sample RadGrid web site to demonstrate a basic implementation. Please make the suggested changes and let me know about the result.

Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Felice
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Share this question
or