locked
Filtering is not working for columns(Label Number,Sequence Number & Media Type) in SharePoint Gridview RRS feed

  • Question

  • Hi,

    I have gridview data where i am applying sorting and filtering.Sorting is working fine.But filtering have some issue for few columns,few columns are working for filtering.working Filtering columns(Description & Address).Not working Filtering columns(Label Number,Sequence Number & Media Type)

    Below are my code.

    <table>
        <tr>
            <td>
                <SharePoint:SPGridView Width="100%" ID="grdInventoryByCycle" CellSpacing="2" runat="server"
                    AutoGenerateColumns="false">
                    <Columns>
                        <SharePoint:SPBoundField HeaderStyle-BackColor="#BDA65A" HeaderText="Label Number"
                            DataField="Label Number" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Left">
                        </SharePoint:SPBoundField>
                        <asp:BoundField HeaderStyle-BackColor="#BDA65A" HeaderText="Description" HtmlEncode="false"
                            HeaderStyle-HorizontalAlign="Center" DataField="Description" ItemStyle-HorizontalAlign="Left" />
                        <SharePoint:SPBoundField HeaderStyle-BackColor="#BDA65A" HeaderText="Sequence Number"
                            HeaderStyle-HorizontalAlign="Center" DataField="Sequence Number" ItemStyle-HorizontalAlign="Left">
                        </SharePoint:SPBoundField>
                        <SharePoint:SPBoundField HeaderStyle-BackColor="#BDA65A" HeaderText="Backup Date"
                            HeaderStyle-HorizontalAlign="Center" DataField="Backup Date" ItemStyle-HorizontalAlign="Left">
                        </SharePoint:SPBoundField>                   
                        <SharePoint:SPBoundField HeaderStyle-BackColor="#BDA65A" HeaderText="Address" HeaderStyle-HorizontalAlign="Center"
                            DataField="Address" ItemStyle-HorizontalAlign="Left">
                        </SharePoint:SPBoundField>
                        <SharePoint:SPBoundField HeaderStyle-BackColor="#BDA65A" HeaderText="Media Type"
                            HeaderStyle-HorizontalAlign="Center" DataField="Media Type" ItemStyle-HorizontalAlign="Left">
                        </SharePoint:SPBoundField>
                    </Columns>
                </SharePoint:SPGridView>
                <asp:ObjectDataSource ID="gdrsDataSource" runat="server"></asp:ObjectDataSource>
            </td>
        </tr>
    </table>

    protected voidPage_Load(objectsender, EventArgse)

            {

                grdInventoryByCycle.DataSourceID = gdrsDataSource.ID;

                gdrsDataSource.SelectMethod =

    "BindGrid";

                gdrsDataSource.TypeName =

    this.GetType().AssemblyQualifiedName;

                gdrsDataSource.ObjectCreating +=

    newObjectDataSourceObjectEventHandler(gdrsDataSource_ObjectCreating);

           

               

    //Filtering


                gdrsDataSource.Filtering +=

    newObjectDataSourceFilteringEventHandler(gdrsDataSource_Filtering);                                   

                grdInventoryByCycle.AllowFiltering =

    true;

                grdInventoryByCycle.FilterDataFields =

    "Label Number,Description,Batch Number,Backup Date,Address,Media Type";

                grdInventoryByCycle.FilteredDataSourcePropertyName =

    "FilterExpression";

                grdInventoryByCycle.FilteredDataSourcePropertyFormat =

    "{1} = '{0}'";

                grdInventoryByCycle.PagerTemplate =

    null;

               

    //Sorting


                grdInventoryByCycle.AllowSorting =

    true;

                grdInventoryByCycle.Sorting +=

    newGridViewSortEventHandler(grdInventoryByCycle_Sorting);

                btnExportToExcel.ImageUrl =

    SPContext.Current.Web.Url + "/Style%20Library/BMS/Images/Excel-icon.png";

            }

    public DataTable BindGrid()
            {
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                        {
                            using (SPWeb web = site.OpenWeb())
                            {
                                #region Datatable creation

                                dtInventory.Columns.Add("Label Number", typeof(string));
                                dtInventory.Columns.Add("Description", typeof(string));
                                dtInventory.Columns.Add("Sequence Number", typeof(string));
                                dtInventory.Columns.Add("Backup Date", typeof(string));
                                dtInventory.Columns.Add("Address", typeof(string));                                                       
                                dtInventory.Columns.Add("Media Type", typeof(string));
                                                           
                                #endregion

                                SPList list = web.Lists.TryGetList("Tape Inventory");

                                if (list != null)
                                {
                                    SPListItemCollection items = list.GetItems();                               

                                    foreach (SPListItem item in items)
                                    {
                                        DataRow dr = dtInventory.NewRow();
                                        dr["Label Number"] = item["Title"].ToString();
                                        dr["Description"] = item["Description"].ToString();
                                        dr["Sequence Number"] = item["Batch_x0020_Number"].ToString();
                                        dr["Backup Date"] = Convert.ToDateTime(item["Backup_x0020_Date"]).ToShortDateString();                                   
                                        dr["Address"] = item["Address"].ToString();
                                        dr["Media Type"] = item["Media_x0020_Type"].ToString();                                   
                                                                           
                                        dtInventory.Rows.Add(dr);
                                    }
                                }
                            }
                        }
                    });
                }
                catch (Exception ex)
                {
                    ex.Message.ToString();
                }
                if (dtInventory.Rows.Count > 0)
                {
                    return dtInventory;
                }
                else
                {
                    grdInventoryByCycle.EmptyDataText = "No Records found based on the selection..";
                    return null;
                }
            }

    protected

    voidgdrsDataSource_ObjectCreating(objectsender, ObjectDataSourceEventArgse)

            {

                e.ObjectInstance =

    this;

            }

           

    privatevoidgdrsDataSource_Filtering(objectsender, ObjectDataSourceFilteringEventArgse)

            {

                ViewState[

    "FilterExpression"] = ((ObjectDataSourceView)sender).FilterExpression;

            }

           

    protectedvoidgrdInventoryByCycle_Sorting(objectsender, GridViewSortEventArgse)

            {

               

    try


                {

                   

    if(ViewState["FilterExpression"] != null)

                    {

                        gdrsDataSource.FilterExpression = (

    string)ViewState["FilterExpression"];

                    }

                }

               

    catch(Exceptionex)

                {

                   

    //Logger.LogException("Occurred in grdTADashboard_Sorting() of TAOAFDashboardUserControl -- " + ex);


                }

            }

    Monday, May 9, 2016 7:27 AM

All replies