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.
5 Answers, 1 is accepted
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.
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
rdgrdDetails.Columns(3).IsVisible = False
rdgrdDetails.Columns(5).IsVisible = True
If CDbl(radGridView1.Rows(0).Cells("Price").Value) = 0 Then
radGridView1.Columns("Price").HeaderText = String.Empty
radGridView1.Columns("Price").HeaderText = "Price"
We have the page as
Screen Shot link
But we need the page to be like the below screenshot
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.
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".
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.