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

Showing Yes/No for Boolean in Grid

14 Answers 2878 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lynn
Top achievements
Rank 2
Lynn asked on 06 Jun 2010, 12:18 AM
I hate to waste your time because I know you must have answered this question before.  I did find one answer to it in the forums, but the example did not work on my system, so..........

I would like to display "Yes" or "No" for a SQL Server Boolean column in the RadGrid instead of a checkbox (your checkboxes in the grid are so "faded" that it is very difficult to tell if they are checked or not) or instead of "True"/"False".

I would prefer to write the code for this in the VB code behind page if possible. If not, I can add it to the .aspx page if needed.

The Boolean field is named "TextActive", although it really doesn't matter.

Thanks in advance for your assistance.

Lynn

14 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 07 Jun 2010, 07:07 AM
Hello Lynn ,

I prefer following approach in order to show Yes/No based on Boolean value. I used a label in ItemTemplate of GridTemplateColumn.

ASPX:
  
<telerik:GridTemplateColumn>  
    <ItemTemplate>  
        <asp:Label ID="Label1" runat="server" Text='<%# Convert.ToBoolean(Eval("TextActive")) == true ? "Yes" : "No" %>'></asp:Label>  
    </ItemTemplate>  
</telerik:GridTemplateColumn> 



-Shinu.
0
Lynn
Top achievements
Rank 2
answered on 08 Jun 2010, 02:59 PM
Is there not a way to accomplish this in the code behind page?  There are certain properties in this grid that I MUST modify at run-time including the text values used in this Yes/No column.  Why can't I define this column at run-time from the code behind page while I am doing everything else?
0
Accepted
Shinu
Top achievements
Rank 2
answered on 09 Jun 2010, 07:32 AM
Hello Lynn,

Here is code snippet for accomplishing same. You can access the cell values in ItemDataBound and modify it according to required funcionality.

ASPX:
<telerik:GridBoundColumn UniqueName="TextActive" DataField="TextActive" HeaderText="TextActive"
</telerik:GridBoundColumn> 

VB.Net:

 Protected
 Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs) 
    If TypeOf e.Item Is GridDataItem Then 
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem) 
        If item("isapproved").Text = "True" Then 
                'your code 
            item("isapproved").Text = "Yes" 
        Else 
                'your code 
            item("isapproved").Text = "No" 
        End If 
    End If 
 End Sub 
 

Regards,
Shinu.





0
Lynn
Top achievements
Rank 2
answered on 10 Jun 2010, 05:37 AM
Shinu,

I thank you for the time spent on this, but I don't understand:

(a) How this helps me.  I have to be able to modify certain items in the column defintions at run time (from the code behind page). yet you are still defining the column in the .aspx page.  Can I define the column in the code behind page as shown below? I have this code in the Page_Init sub.
            boundcolumn = New GridBoundColumn  
            Me.RadGrid1.Columns.Add(boundcolumn)  
            boundcolumn.UniqueName = "TextActive" 
            boundcolumn.DataField = "TextActive" 
            boundcolumn.HeaderText = "TextIt" 
            boundcolumn.Visible = True 
            boundcolumn.HeaderTooltip = "" 
            boundcolumn = Nothing 
 

If I do that, then how do I include the code you wrote for the data manipulation portion?  I've included your code behind page code below for easy reference.  I'm not a stupid person, but I don't understand what you are doing below relative to the
item("isapproved").Text code.  I'm guessing that this is the name of the column in some other example, but when I use this code and change the "isapproved" to my own value "TextActive" it does not work.  The column in the Grid still contains the actual words True or False instead of "Yes" or "No".

Basically, I guess I don't get it.  Sorry.
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)    
    If TypeOf e.Item Is GridDataItem Then    
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)    
        If item("isapproved").Text = "True" Then    
                'your code    
            item("isapproved").Text = "Yes"    
        Else    
                'your code    
            item("isapproved").Text = "No"    
        End If    
    End If    
 End Sub    
    
 

0
Accepted
Dimo
Telerik team
answered on 10 Jun 2010, 12:15 PM
Hi Lynn,

The RadGrid's ItemDataBound handler is not related to the way you create the columns - declaratively or programmatically. The ItemDataBound event is related to data rows, not columns.

item("isapproved") is a way to access the content of a data cell by using the column's unique name.

http://www.telerik.com/help/aspnet-ajax/grdaccessingcellsandrows.html

Greetings,
Dimo
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Lynn
Top achievements
Rank 2
answered on 10 Jun 2010, 02:31 PM
Dimo,

Thanks for the reply.  I'm trying to understand.  I made changes to the code behind to reflect your recommendations to no avail.  However, it isn't the actual code in the _ItemDataBound event that doesn't seem to be working, it appears that the _ItemDataBound event code is not being executed.

Below is the code from the code behind page.
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init  
        If Not Page.IsPostBack Then  
            Dim hypercolumn As GridHyperLinkColumn  
            Dim boundcolumn As GridBoundColumn  
            Dim checkboxcolumn As GridCheckBoxColumn  
 
            hypercolumn = New GridHyperLinkColumn  
            Me.RadGrid1.Columns.Add(hypercolumn)  
            hypercolumn.HeaderText = "Edit" 
            hypercolumn.UniqueName = "Approve" 
            hypercolumn.Text = "<img border=""0"" alt=""View"" src=""../Icons/pencil_16.png"" />" 
            hypercolumn.DataNavigateUrlFields = New String() {"TextID"}  
            hypercolumn.DataNavigateUrlFormatString = sEditRecRedirect & "{" & sEditRecKeyField & "}"  
            hypercolumn.HeaderTooltip = "Displays a page for editing the selected Topic record" 
            hypercolumn = Nothing 
 
            boundcolumn = New GridBoundColumn  
            Me.RadGrid1.Columns.Add(boundcolumn)  
            boundcolumn.UniqueName = "TextName" 
            boundcolumn.DataField = "TextName" 
            boundcolumn.HeaderText = "Name" 
            boundcolumn.Visible = True 
            boundcolumn.HeaderTooltip = "The name of this Topic" 
            boundcolumn = Nothing 
 
            checkboxcolumn = New GridCheckBoxColumn  
            Me.RadGrid1.Columns.Add(checkboxcolumn)  
            checkboxcolumn.UniqueName = "TextActive" 
            checkboxcolumn.DataField = "TextActive" 
            checkboxcolumn.HeaderText = "Active" 
            checkboxcolumn.HeaderTooltip = "Indicates if this text item is to be used from the database" 
            checkboxcolumn = Nothing 
 
            boundcolumn = New GridBoundColumn  
            Me.RadGrid1.Columns.Add(boundcolumn)  
            boundcolumn.UniqueName = "TextExtra" 
            boundcolumn.DataField = "TextActive" 
            boundcolumn.HeaderText = "Manipulate" 
            boundcolumn.Visible = True 
            boundcolumn = Nothing 
 
        End If  
    End Sub  
    Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs)  
        If TypeOf e.Item Is GridDataItem Then  
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)  
            Me.Label1.Text = item("TextExtra").Text & "<br>"  
            If item("TextExtra").Text = "True" Then  
                'your code    
                item("TextExtra").Text = "Yes" 
            Else  
                'your code    
                item("TextExtra").Text = "No" 
            End If  
        End If  
    End Sub  
    Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource  
        RadGrid1.DataSource = GetDataTable("SELECT * FROM TransText_Topics_List WHERE Text_AccountID=" & Session("AccountID") & " AND TextApplication='THDi PWS' AND TextType='Topic' ORDER BY TextApplication, TextType, TextSubType, TextName")  
    End Sub  
    Public Function GetDataTable(ByVal query As String) As DataTable  
        Dim ConnString As String = Application("MySiteDBConnectString")  
        Dim conn As SqlConnection = New SqlConnection(ConnString)  
        Dim adapter As SqlDataAdapter = New SqlDataAdapter  
        adapter.SelectCommand = New SqlCommand(query, conn)  
        Dim table1 As New DataTable  
        conn.Open()  
        Try  
            adapter.Fill(table1)  
        Finally  
            conn.Close()  
        End Try  
        Return table1  
    End Function  
    Protected Sub CreateNew_Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateNew_Button.Click  
        Response.Redirect(sNewRecRedirect)  
    End Sub  
End Class  
 

In the Page_Init event code, you will see that the last column created is one with the unique name of "TextExtra".  This is the column that I am trying to modify the grid column text for.

You will also see in the _ItemDataBound event code, that each time through I am changing the text value of a label on the web page to include the data values from all rows (complete with a <br> for each value so they can all be viewed).

I have attached a small screen capture of the resulting grid.  You will see the "TextExtra" column still contains "True" instead of "Yes".  If you look near the bottom of the screen capture, you will see a field that simply says "Label".  This is "Label1" which I am supposedly setting to the various data values -- but which still contains the initial value.  From this, I can only assume that the _ItemDataBound event is not firing.

What am I doing wrong to prevent this from working?

The actual definition of the grid from the .aspx page is:

                <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" Width="725px" Height="200px" EnableEmbeddedSkins="true" BackColor="#11161D" Skin="Black" > 
 
                </telerik:RadGrid> 
 

Thanks!

Lynn
0
Lynn
Top achievements
Rank 2
answered on 10 Jun 2010, 03:27 PM
Thank you for your help.  I've marked this as closed.  I still had a problem after the last round of communications, but resolved it a few minutes ago here.  In the sample code you supplied for the _ItemDataBound event, the "Handles RadGrid1.ItemDataBound" was absent from the example.  To be honest, I should have seen that sooner.  Anyway, after fixing that problem, the issue is resolved.  Thanks again and I mean that (I'm not being mean and nasty).

Lynn
0
Rahul
Top achievements
Rank 1
answered on 21 Jan 2014, 11:56 AM
Hi Shinu,
          Same problem in my case but i want to do it client side. please provide a solutions.

Thanks,
Rahul
0
Shinu
Top achievements
Rank 2
answered on 21 Jan 2014, 12:17 PM
Hi Rahul,

You can easily set the Boolean as Yes/No in the ASPX page by using a GridTemplateColumn as follows, if this doesn't help, elaborate on your requirement and provide code snippet if necessary:

ASPX:
<telerik:GridTemplateColumn>
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Convert.ToBoolean(Eval("IsTrue")) == true ? "Yes" : "No" %>'></asp:Label>
    </ItemTemplate>
</telerik:GridTemplateColumn>

 Thanks,
Shinu
0
Rahul
Top achievements
Rank 1
answered on 22 Jan 2014, 04:41 AM
Hi Shinu,
              Thanks for Reply, It works fine my side.
Thanks,
Rahul
0
Umesh
Top achievements
Rank 1
answered on 14 Mar 2014, 12:38 PM
hi Shinu
How can i do in Razor view i am using kendoUI.
0
Umesh
Top achievements
Rank 1
answered on 14 Mar 2014, 12:39 PM
i am using Kendo Grid.
0
rahul
Top achievements
Rank 1
answered on 18 Apr 2014, 11:26 AM
Hi Shinu,
     How can  get this column value  using onRowselected event on client side. please provide solutions.
Thanks,
Rahul
0
Shinu
Top achievements
Rank 2
answered on 21 Apr 2014, 07:06 AM
Hi Rahul,

Please try the following code snippet to get the selected row value of label.

ASPX:
<ClientSettings Selecting-AllowRowSelect="true">
    <ClientEvents OnRowSelected="OnRowSelected" />
</ClientSettings>

JS:
<script type="text/javascript">
    function OnRowSelected(sender, args) {     
        var grid = sender;
        var masterTable = grid.get_masterTableView();
        var length = masterTable.get_selectedItems().length;
        for (i = 0; i < length; i++) {
            var row = masterTable.get_selectedItems()[i];
            var element = row.get_element();
            var labelValue = $telerik.findElement(element, "Label1").innerHTML;
            alert(labelValue); //Get the Boolean value
        }
    }
</script>

Thanks,
Shinu
Tags
Grid
Asked by
Lynn
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Lynn
Top achievements
Rank 2
Dimo
Telerik team
Rahul
Top achievements
Rank 1
Umesh
Top achievements
Rank 1
rahul
Top achievements
Rank 1
Share this question
or