GridCheckBoxColumn has a literal filter value

3 posts, 0 answers
  1. Igor
    Igor avatar
    24 posts
    Member since:
    May 2013

    Posted 26 Mar 2015 Link to this post

    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. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 26 Mar 2015 in reply to Igor Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Igor
    Igor avatar
    24 posts
    Member since:
    May 2013

    Posted 29 Mar 2015 Link to this post

    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.

Back to Top