Convert grid column value from Boolean to customized string And applying filter on column

5 posts, 0 answers
  1. Aditi
    Aditi avatar
    18 posts
    Member since:
    Jun 2012

    Posted 07 Jun 2012 Link to this post

    Hi,

    I have a radgrid, which has one of the column say Status. The datatype of value for this column Status is bool.
    I am setting text of each cell of Status column to depending on some conditions, in the rdgrid_ItemDataBound event.
    string str = "A", "AAB", "ABC", "ABCD"
    So, depending on some specific conditions met, str can be any of the above values.

    If e.item.cell[9].Text == "True"
        e.item.cell[9].Text = "A";

    If e.item.cell[9].Text == "False"
    e.item.cell[9].Text = "AAB";

    If e.item.cell[9].Text == "True" && e.item.cell[7].Text = "False"
    e.item.cell[9].Text = "ABCD";

    I want to apply string filter on this column. Say I enter "ABCD" in the filter for this column of grid from UI, it should give me result for value "ABCD" only.

    Thanks


  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 07 Jun 2012 Link to this post

    Hi Aditi,

    You cannot filter a bool field with its text property.I guess you are using CheckBox in Template column.One suggestion is to put an invisible label in the Template Column and bind it with a database field whose text property changes after checking the checkbox columns checked  and updates the value in database. Please check the sample code snippet I tried.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" AllowFilteringByColumn="true" runat="server"
            DataSourceID="SqlDataSource1" AutoGenerateColumns="false"
            onitemdatabound="RadGrid1_ItemDataBound">
        <MasterTableView DataKeyNames="ProductID">
            <Columns>
                <telerik:GridTemplateColumn AllowFiltering="true" DataField="status" >
                    <ItemTemplate>
                        <asp:CheckBox ID="checkbox1" runat="server" Checked='<%#Bind("Discontinued") %>'/>
                        <asp:Label ID="label1" runat="server" Text='<%#Eval("status") %>' Visible="false" ></asp:Label>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    C#:
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem ditem = (GridDataItem)e.Item;       
            CheckBox chk = (CheckBox)ditem.FindControl("checkbox1");
            Label lbl = (Label)ditem.FindControl("label1");
            string datakey = ditem.GetDataKeyValue("ProductID").ToString();
            if (chk.Checked)
            {
                string status = "A";
                chk.Text = "A";
                conn.Open();
                string updateQuery = "Update Products set status='" + status + "' where ProductID='" + datakey + "'";
                SqlCommand.CommandText = updateQuery;
                SqlCommand.Connection = conn;
                SqlCommand.ExecuteNonQuery();
                conn.Close();
            }
            else
            {
                string status = "AAB";
                chk.Text = "AAB";
                conn.Open();
                string updateQuery = "Update Products set status='" + status + "' where ProductID='" + datakey + "'";
                SqlCommand.CommandText = updateQuery;
                SqlCommand.Connection = conn;
                SqlCommand.ExecuteNonQuery();
                conn.Close();
            }
        }
    }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Aditi
    Aditi avatar
    18 posts
    Member since:
    Jun 2012

    Posted 07 Jun 2012 Link to this post

    Hi Shinu,

    Thanks for your reply. However I am using gridbound column and the field it self has the Boolean data coming from database.
    i.e. datafield is assigned to column of a table which has data type as boolean.
    So, I was tying to convert this boolean value into string. However as the data for column is still bool and the text entered in filter is string value thus results in conversion error.

    I am trying to find a work around for this column.

    Thanks,
    Aditi
  5. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 08 Jun 2012 Link to this post

    Hello Aditi,

    Please check below demo.

    <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
  6. access
    access avatar
    3 posts
    Member since:
    Sep 2012

    Posted 26 Jul 2014 in reply to Jayesh Goyani Link to this post

    thanks .its working.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017