locked
Use same method to filter and sort datagrid RRS feed

  • Question

  • User673640389 posted

    I have a datagrid, that is set up to sort rows based on the built in sorting functionality of the data grid, where you can click on column headers to sort the rows.  I also have a set of controls above the data grid where the user can enter values to search/filter the rows in the grid. 

    I wanted to tie both the onclick event of the search button, and the sorting event back to the same method in my code.  My problem is that the data grid sort event takes a "gridviewsorteventargs" parameter, which the onclick event of the search button does not pass.

    I tried to make the parameter optional, but don't know how to set the default value for a gridviewsorteventarg. Is there a way to get this working....or can I pass a default "gridviewsorteventarg" in the onclick event of my button just to satisfy this routine?

    Protected Sub exceptions_grid_SortFilter(sender As Object, e As GridViewSortEventArgs)

    Wednesday, December 5, 2018 4:34 PM

All replies

  • User283571144 posted

    Hi AaronR___,

    According to your description ,I have made a sample here. If you want to use sort and filter in the same method,you just need to write the filter and sort method both in Button click event without using DataGrid sort event and its parameter.When you click the button ,it will both filter and sort.Here is the code ,I hope it will help you.

    Datagrid.aspx:

    <form id="form1" runat="server">
           
     <div>
                  Number:<asp:TextBox ID="TextBox1" runat="server" AutoCompleteType="Disabled"></asp:TextBox>
                 <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                
                 <asp:DataGrid ID="d1" runat="server"  Height="411px" Width="470px" ></asp:DataGrid>
            </div> 
    
        </form>

    Datagrid.aspx.cs:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack == true)
                {
    
                    string sql = "select * from dd";
                    DataTable dt = SqlHelper.ExecuteDataTable(sql);
                    d1.DataSource = dt;
                    d1.DataBind();
                    
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                
                    string s1 = this.TextBox1.Text.Trim();
    
                    string sql = "select * from dd where Number like @number";
    
                    DataTable dt = SqlHelper.ExecuteDataTable(sql, new SqlParameter("@number", "%" + s1 + "%"));
    // using Fuzzy query
     
                    DataView dv = new DataView(dt);
                    dv.Sort = "Number asc";
                //ascend the data  by column Number or you can write 'Number desc' to descend the data
                    DataTable dt2 = dv.ToTable();
                    d1.DataSource = dt2;
                    d1.DataBind();
    
     
                
            }

    Result:

    What is more ,you can also sort the data by writing filter method in Datagrid SortCommand event.When you click on column headers ,it will also filter and sort.Here is the code ,I hope it will help you.

    Datagrid.aspx:

    <body>
        <form id="form1" runat="server">
            <div>
                 Number:<asp:TextBox ID="TextBox1" runat="server" AutoCompleteType="Disabled"></asp:TextBox>
                 <asp:DataGrid ID="d1" runat="server" AllowSorting="true"  OnSortCommand="Unnamed_SortCommand" Height="411px" Width="470px" ></asp:DataGrid>
            </div>
        </form>
    </body>

    Datagrid.aspx.cs:

     public partial class data : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack == true)
                {
                    string sql = "select * from dd";
                    DataTable dt = SqlHelper.ExecuteDataTable(sql);
                    d1.DataSource = dt;
                    d1.DataBind();                
                }
    
            }
    
            protected void Unnamed_SortCommand(object source, DataGridSortCommandEventArgs e)
            {
               
              string s = this.TextBox1.Text.Trim();
               
                string sql = "select * from dd where Number like @number";
                           
                DataTable dt = SqlHelper.ExecuteDataTable(sql,new SqlParameter("@number","%"+s+"%"));  
    // doing Fuzzy Searches 
               
                DataView dv = new DataView(dt);
    
                // The DataView provides an easy way to sort. Simply set the  Sort property with the name of the field to sort by.
                dv.Sort = e.SortExpression
                // Rebind the data source and specify that it should be sorted
                // by the field specified in the SortExpression property.
                d1.DataSource = dv;
                d1.DataBind(); 
    
            }
    
            }
        }
    

    Result:

    Best Regards,

    Brando

    Thursday, December 6, 2018 10:16 AM
  • User673640389 posted

    Woah, what a thorough response!  Thank you.

    If I'm understanding correctly, the first example you show doesn't utilize the Data View's built in sorting functionality, where you can click on the column heading to sort.  I could specify sort in another field rather than using the DataView's sort event, but I think being able to click on the column headings to sort would make for a better user experience. 

    With the 2nd example, there doesn't seem to be a way to get the data to filter without also sorting.  Do you know of a way that a button could exist, next to the number text box, that when pressed calls is able to run your  protected void Unnamed_SortCommand(object source, DataGridSortCommandEventArgs e) routine?




    Tuesday, December 11, 2018 3:49 PM
  • User283571144 posted

    Hi AaronR___,

    Could you please tell me which you have used now? Datagrid or gridview?  As far as I knwo, the datagrid doesn't have the gridviewsorteventargs.

    Since these two control have different control definition, the code will be different. 

    If you could provide more details information, it will be more easily for us to understand your issue and find out the solution.

    Best Regards,

    Brando

    Friday, December 14, 2018 1:44 AM
  • User673640389 posted

    Sorry for late reply...I am using a gridview.

    My code is working now...I just feel like I'm missing a more elegant solution.

    I'll try to describe it in a nutshell:

    PopulateGrid Event: takes a where clause parameter, and a order by parameter, and repopulates gridview accordingly

    Filter button event: constructs a where clause based on data in filter controls, passes this, along with a default sort value to the Populate Grid event

    Gridview Sort event:  constructs an order by clause based on the column that was clicked, it also constructs a where clause just like the filter button event, so that the selected filter values are not lost when a user sorts.

    So the duplication is taking place in that both the filter button and gridview sort event construct the where clause.  I suppose I could make a new function that simply returns a where clause string.

    Wednesday, December 19, 2018 12:38 AM