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

GridCheckBoxColumn has a literal filter value

2 Answers 94 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Igor
Top achievements
Rank 2
Igor asked on 26 Mar 2015, 10:35 AM
Hello, I bind a grid programmatically using NeedDataSource.
Filtering, sorting, paging, all work perfectly.

myDataSource = new ObjectDataSource { ... };
myDataSource.Selecting += SourceSelecting;
...
 
private void SourceSelecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
     e.InputParameters["filterExpression"] = myGrid.MasterTableView.FilterExpression;
}

I have problems when SQL Server BIT columns are bound to a GridCheckBoxColumn and I allow filtering on it.
The problem is that in previous code the filter expression is something like ([MyBoolColumn] = True).
This expression is not valid when I pass it to SQL Server, because True is not a valid bit value (should be 1).

Is there any way to tell the grid to use 0/1 as filter values for GridCheckBoxColumns instead of False/True?

Thanks

2 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 26 Mar 2015, 05:41 PM
Hello Igor,

Could you please try with the below code snippet?

ASPX
<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>


ASPX.CS
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;
    }


Let me know if any concern.

Thanks,
Jayesh Goyani
0
Igor
Top achievements
Rank 2
answered on 29 Mar 2015, 08:18 AM
Jayesh, thanks for the answer.
Unfortunately it does not work.
I created a sample app using you suggestion but I'm not able to attach it here (only images allowed for attachment).
I'll open a support ticket.

Tags
Grid
Asked by
Igor
Top achievements
Rank 2
Answers by
Jayesh Goyani
Top achievements
Rank 2
Igor
Top achievements
Rank 2
Share this question
or