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

How to hide the Headertext column dynamically

5 Answers 1142 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dev
Top achievements
Rank 1
Veteran
Dev asked on 07 Aug 2020, 12:56 PM

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

5 Answers, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 10 Aug 2020, 01:13 PM

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

0
Dev
Top achievements
Rank 1
Veteran
answered on 12 Aug 2020, 01:35 PM

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

0
Nadya | Tech Support Engineer
Telerik team
answered on 14 Aug 2020, 09:16 AM

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

0
Dev
Top achievements
Rank 1
Veteran
answered on 24 Aug 2020, 11:08 AM

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

0
Nadya | Tech Support Engineer
Telerik team
answered on 26 Aug 2020, 01:33 PM

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).

Tags
GridView
Asked by
Dev
Top achievements
Rank 1
Veteran
Answers by
Nadya | Tech Support Engineer
Telerik team
Dev
Top achievements
Rank 1
Veteran
Share this question
or