radgrid filter yes/No, 1/0

4 posts, 0 answers
  1. JJ
    JJ avatar
    147 posts
    Member since:
    May 2010

    Posted 02 Sep 2011 Link to this post

    I have a radgrid the column is a decimal value 1 or 0,

    I have code in ItemDataBound to change 1 to Yes and 0 to No. everything is fine. But when do the filter, Nomattter I put Yes or No as filter, everything returned.
    GridDataItem item = (GridDataItem)e.Item;
      
    if (item["myfield"].Text == "1")
    {
    item["myfield"].Text = "Yes"
    }
    else
    {
    item["myfield"].Text = "No"
    }
    Please help
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2733 posts
    Member since:
    May 2010

    Posted 03 Sep 2011 Link to this post

    Hello,

    http://www.telerik.com/help/aspnet/grid/grdfilteringfordatetimecolumnwithdataformatstring.html // same logic you can apply for your column also

    let me know if any concern.

    Thanks,
    Jayesh Goyani
  3. JJ
    JJ avatar
    147 posts
    Member since:
    May 2010

    Posted 03 Sep 2011 Link to this post

    Hi Jayesh,

    I don't think I got your point. In my case, my grid bound to a class has field column 1/0,  1 means Yes, 0 means No. So when I display the data, I switched 1/0 to Yes/No.  So when I try to do the filter, I type "Yes", I suppose to get Yes only back, but instead I get everything back, which means the filter doesn't work.

    I don't see there are connection between your example and mine case. As long as the grid already displayed Yes/No, when the user type Yes, it should automatically return Yes only, is it?
  4. Jayesh Goyani
    Jayesh Goyani avatar
    2733 posts
    Member since:
    May 2010

    Posted 03 Sep 2011 Link to this post

    Hello,

    Please see below code snippet and let me know if any concern.

    <telerik:RadGrid ID="RadGrid1" runat="server"   AllowFilteringByColumn="true"
               PageSize="2" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemCommand="RadGrid1_ItemCommand"
               AutoGenerateColumns="false" OnItemDataBound="RadGrid1_ItemDataBound">
               <MasterTableView DataKeyNames="ID">
                   <Columns>
                     <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                      
                     </telerik:GridBoundColumn>
                   </Columns>
               </MasterTableView>
           </telerik:RadGrid>
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
     
                if (item["ID"].Text == "1")
                {
                    item["ID"].Text = "Yes";
                }
                else
                {
                    item["ID"].Text = "No";
                }
            }
        }
     
        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.FilterCommandName && ((Pair)e.CommandArgument).First.ToString().Contains("NoFilter") && ((Pair)e.CommandArgument).Second.ToString().Contains("ID"))
            {
                e.Canceled = true;
                GridFilteringItem filterItem = (GridFilteringItem)e.Item;
                string currentPattern = ((TextBox)filterItem["ID"].Controls[0]).Text;
                string filterPattern;
     
                if (currentPattern.ToLower() == "yes")
                {
                    filterPattern = "1";
                }
                else
                {
                    filterPattern = "0";
                }
     
                GridBoundColumn dateColumn = (GridBoundColumn)e.Item.OwnerTableView.GetColumnSafe("OrderDate");
                filterPattern = "[OrderDate] = '" + filterPattern + "'";
                dateColumn.CurrentFilterFunction = GridKnownFunction.EqualTo;
            }
        }
        protected void RadGrid1_NeedDataSource(object sender, EventArgs e)
        {
            dynamic data = new[] { new { ID = 1 }, new { ID = 1 }, new { ID = 0 }, new { ID = 1 } };
     
            RadGrid1.DataSource = data;
        }


    Thanks,
    Jayesh Goyani
Back to Top