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

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

4 Answers 278 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Aditi
Top achievements
Rank 1
Aditi asked on 08 Jun 2012, 12:22 AM
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


4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 08 Jun 2012, 04:46 AM
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.
0
Aditi
Top achievements
Rank 1
answered on 08 Jun 2012, 04:59 AM
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
0
Jayesh Goyani
Top achievements
Rank 2
answered on 08 Jun 2012, 05:55 AM
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
0
access
Top achievements
Rank 1
answered on 26 Jul 2014, 01:11 PM
thanks .its working.
Tags
Grid
Asked by
Aditi
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Aditi
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
access
Top achievements
Rank 1
Share this question
or