locked
How to filter our rows of a datasource in SqlDataSource_Selected event. RRS feed

  • Question

  • User1279376247 posted

    I have a SqlDataSource connected to a Gridview.

    How can I filter out rows of the sqldatasource (in code-behind) on the sqldatasource's Selected event (prior to the Gridview being bound).

    I want to do this based on the value of one of the columns returned in the SqlDataSource.

    I need to do this in code-behind and not by passing a parameter to the SQL.

    Thank you.

    Monday, January 7, 2013 10:20 AM

Answers

  • User1786833724 posted
    SqlDataSource.SelectParameters.Clear();
    
    //and then ...
    SqlDataSourceArticole.SelectParameters.Add("@id", id);
    SqlDataSourceArticole.SelectParameters["id"].DefaultValue = id.ToString();
    
    //Other way...
    ControlParameter CategoryID = new ControlParameter();  
    CategoryID.Name = "CategoryID";  
    CategoryID.ControlID = "DropDownList1";  
    CategoryID.PropertyName = "SelectedValue";  
              
    SqlDataSource2.SelectParameters.Clear();  
    SqlDataSource2.SelectParameters.Add(CategoryID);  
    SqlDataSource2.Select(DataSourceSelectArguments.Empty);  
      
    GridView1.DataSource = SqlDataSource2;  
    GridView1.DataBind();  

    Or...

    protected void SqlResDetail_Selecting(object sender, System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs e)
    {
       SqlParameter sp = new SqlParameter("ResId", SelectedReservationId);
       sp.Direction = ParameterDirection.Input;
       e.Command.Parameters.Add(sp);
    }




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 7, 2013 10:58 AM

All replies

  • User-483528683 posted

    Hi 

    You can use LINQ for that.

    once we you can data into dataset you can filer whatever you want do filter.

    here is some example of LINQ

    http://msdn.microsoft.com/en-us/library/vstudio/bb397927.aspx

    http://stackoverflow.com/questions/3117003/c-sharp-linq-select-from-list

    Thanks

    http://dakvikas.webs.com

    Monday, January 7, 2013 10:51 AM
  • User1786833724 posted
    SqlDataSource.SelectParameters.Clear();
    
    //and then ...
    SqlDataSourceArticole.SelectParameters.Add("@id", id);
    SqlDataSourceArticole.SelectParameters["id"].DefaultValue = id.ToString();
    
    //Other way...
    ControlParameter CategoryID = new ControlParameter();  
    CategoryID.Name = "CategoryID";  
    CategoryID.ControlID = "DropDownList1";  
    CategoryID.PropertyName = "SelectedValue";  
              
    SqlDataSource2.SelectParameters.Clear();  
    SqlDataSource2.SelectParameters.Add(CategoryID);  
    SqlDataSource2.Select(DataSourceSelectArguments.Empty);  
      
    GridView1.DataSource = SqlDataSource2;  
    GridView1.DataBind();  

    Or...

    protected void SqlResDetail_Selecting(object sender, System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs e)
    {
       SqlParameter sp = new SqlParameter("ResId", SelectedReservationId);
       sp.Direction = ParameterDirection.Input;
       e.Command.Parameters.Add(sp);
    }




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 7, 2013 10:58 AM
  • User3866881 posted

    How can I filter out rows of the sqldatasource (in code-behind) on the sqldatasource's Selected event (prior to the Gridview being bound).

    Hi,

    You don't need to use SqlDataSource to cope with Filtering problems.

    You have to do that manually to do with the help of SqlDataAdapter.Fill into a DataTable and then re-bind to the GridView again and again.

    For more you can see this thread:

    http://www.codeproject.com/Articles/26969/GridView-Multiple-Filter-AJAX-Control

    Tuesday, January 8, 2013 12:01 AM