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

radgrid filter yes/No, 1/0

3 Answers 57 Views
Grid
This is a migrated thread and some comments may be shown as answers.
JJ
Top achievements
Rank 1
JJ asked on 02 Sep 2011, 09:07 PM
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

3 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 03 Sep 2011, 06:17 AM
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
0
JJ
Top achievements
Rank 1
answered on 03 Sep 2011, 09:35 AM
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?
0
Jayesh Goyani
Top achievements
Rank 2
answered on 03 Sep 2011, 01:39 PM
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
Tags
Grid
Asked by
JJ
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
JJ
Top achievements
Rank 1
Share this question
or