How to hide the Headertext column dynamically

6 posts, 0 answers
  1. n/a
    n/a avatar
    72 posts
    Member since:
    Jan 2018

    Posted 07 Aug Link to this post

    Hi Team,
             We are using the windows application (vb.net). In our project, we need to hide one of the header text columns (Price) in the RadGridView dynamically. We are setting the condition to hide and show the HeaderText. If the value is "0" the Headertext (Price) should be hidden. If the value is 1 it should be visible. Can you please help with it. Screenshot mentioned below for your reference.

     

    Screen Shot link

  2. Nadya
    Admin
    Nadya avatar
    378 posts

    Posted 10 Aug Link to this post

    Hello Rick,

    In order to customize the look of the cells depending on some conditions, it is suitable to use formatting events. The CellFormatting event is used to access and change the styles of the data cells while the ViewCellFormatting event is fired for all cells. More information you can find here: https://docs.telerik.com/devtools/winforms/controls/gridview/cells/formatting-cells

    Below is a sample example that demonstrates how you can achieve the desired behavior:

    Private Sub RadGridView1_ViewCellFormatting(ByVal sender As Object, ByVal e As CellFormattingEventArgs)
        If CDbl(radGridView1.Rows(0).Cells("Price").Value) = 0 Then
            radGridView1.Columns("Price").HeaderText = String.Empty
        Else
            radGridView1.Columns("Price").HeaderText = "Price"
        End If
    End Sub

    I hope this helps. Let me know if you have other questions.

    Regards,
    Nadya
    Progress Telerik

  3. n/a
    n/a avatar
    72 posts
    Member since:
    Jan 2018

    Posted 12 Aug in reply to Nadya Link to this post

    Hi Nadya,

    Thanks for the reply. On the dropdown, if the selected index is 0, the 3rd column should be visible "True" & 5th column should be visible "False" on the Radgrid

    On the dropdown, if the selected index is 1, the 3rd column should be visible "False" & 5th column should be visible "True" on the Radgrid

    To perform the above process we have written a function & not has an event (ViewCellFormatting). 

    While writing the below code for the function, we get an error as "Object Reference not set to an instance of an object". How can we handle this?
    Code Snippet :

     Function PopulateHistory(SelectedIndex As Integer) 
                If SelectedIndex = 0 Then
                        rdgrdDetails.Columns(3).IsVisible = True              
                        rdgrdDetails.Columns(5).IsVisible = False
               Else               
                        rdgrdDetails.Columns(3).IsVisible = False
                        rdgrdDetails.Columns(5).IsVisible = True 
                End If      
        End Function

     
    If CDbl(radGridView1.Rows(0).Cells("Price").Value) = 0 Then
    radGridView1.Columns("Price").HeaderText = String.Empty
    Else
    radGridView1.Columns("Price").HeaderText = "Price"
    End If

    We have the page as

    Screen Shot link

    But we need the page to be like the below screenshot

    Screen Shot link

  4. Nadya
    Admin
    Nadya avatar
    378 posts

    Posted 14 Aug Link to this post

    Hello, Rick,

    According to the provided information so far, it seems that you have RadDropDownList, and based on the selected index you would like to hide columns, not just the header text. In order to achieve the described scenario, I can suggest you subscribe to the SelectedIndexChanged event of RadDropDownList that fires when the selected index has successfully changed. Thus, you can change the visibility of the columns as per your requirement. Please refer to the following code snippet:

    Private Sub RadDropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.Data.PositionChangedEventArgs)
        If Me.radDropDownList1.SelectedIndex = 0 Then
            Me.radGridView1.Columns(2).IsVisible = True
            Me.radGridView1.Columns(4).IsVisible = False
        ElseIf Me.radDropDownList1.SelectedIndex = 1 Then
            Me.radGridView1.Columns(2).IsVisible = False
            Me.radGridView1.Columns(4).IsVisible = True
        End If
    End Sub

    I hope this helps. If you need further assistance please let me know.

    Regards,
    Nadya
    Progress Telerik

  5. n/a
    n/a avatar
    72 posts
    Member since:
    Jan 2018

    Posted 24 Aug in reply to Nadya Link to this post

    Hi Nadya,

           Thanks for your reply. We tried writing with the SelectedPageChanged event. If the data-table(data-source) count is more than "0" this code is working good. When the data-table count is "0", it throws an exception. Screenshot mentioned below for your reference. But we need the same condition to be working when the records count is "0".

    Screen Shot link

    Screen Shot link

  6. Nadya
    Admin
    Nadya avatar
    378 posts

    Posted 26 Aug Link to this post

    Hi, Rick,

    You can add an additional if-clause in order to check whether there are records in the data table or not. If you do not have records in the data source you should ensure that you have columns created although they might be empty (with no records). The error occurs if there are no columns created in the grid but you try to access them. Could you please make sure that you have columns in grid before trying to hide some of them.

    Let me know if you need further assistance.

    Regards,
    Nadya
    Progress Telerik

    Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive , special prizes and more, for FREE?! Register now for DevReach 2.0(20).

Back to Top