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

Image in Header Problem

1 Answer 132 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Xavier Soares
Top achievements
Rank 2
Xavier Soares asked on 10 Sep 2008, 04:40 PM
Hello,

After your previous support, I can add images to the row header.

Now I'm having some problems when I activate the group by funcionalities. For example, after spending some time analising the code, I founded that if I want to use images in the header row, I need to change ShowRowHeader =  false.

Now I'm having another problem. In same Grids, I get the error:  "Object reference not set to an instance of an object." in the GREEN row

Here Is the code that i'm using:

Dim header As GridTableHeaderRowElement = TryCast(TryCast(Me.DetailedProposals.GridElement, GridTableElement).TableBodyElement.Children(0), GridTableHeaderRowElement)

Me.DetailedProposals.ImageList = Me.ImageList1

For Each element As Telerik.WinControls.RadElement In header.Children

Select Case TryCast(element, GridHeaderCellElement).Text

Case "Molde"

TryCast(element, GridHeaderCellElement).ImageIndex = 0

TryCast(element, GridHeaderCellElement).Text = ""

Case "Print"

TryCast(element, GridHeaderCellElement).ImageIndex = 1

TryCast(element, GridHeaderCellElement).Text = ""

Case "EMail"

TryCast(element, GridHeaderCellElement).ImageIndex = 2

TryCast(element, GridHeaderCellElement).Text = ""

Case "Intranet"

TryCast(element, GridHeaderCellElement).ImageIndex = 3

TryCast(element, GridHeaderCellElement).Text = ""

End Select

element.Alignment = ContentAlignment.MiddleCenter

Next


Can you help? This code works in other radgrid and all columns have name/text

Thank You.
Luis Mauricio

1 Answer, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 11 Sep 2008, 04:13 PM
Hello Luis Mauricio,

Thank you for contacting us.

This exception is thrown because when grouping is applied, GridTableHeaderRowElement contains indent cells. These cells can't be cast to a GridHeaderCellElement. Therefore, you should check if the cast operation returns Nothing. Consider the code snippet below:

For Each element As Telerik.WinControls.RadElement In header.Children 
    Dim HeaderCell As GridHeaderCellElement = TryCast(element, GridHeaderCellElement) 
        If HeaderCell IsNot Nothing Then 
            Select Case HeaderCell.Text 
                Case "Molde" 
                HeaderCell.ImageIndex = 0 
                HeaderCell.Text = "" 
            Case "Print" 
                HeaderCell.ImageIndex = 1 
                HeaderCell.Text = "" 
            Case "EMail" 
                HeaderCell.ImageIndex = 2 
                HeaderCell.Text = "" 
            Case "Intranet" 
                HeaderCell.ImageIndex = 3 
                HeaderCell.Text = "" 
            End Select 
            element.Alignment = ContentAlignment.MiddleCenter 
        End If 
    Next 

In our latest release we have added a ViewCellFormatting event. This event fires every time a system cell needs to change its state. Maybe, processing this event will be a better choice for this task. See the following sample:

    Private Sub ViewCellFormatting(ByVal sender As ObjectByVal e As CellFormattingEventArgs) Handles RadGridView.ViewCellFormatting 
        Dim HeaderCell As GridHeaderCellElement = TryCast(e.CellElement, GridHeaderCellElement) 
        If HeaderCell IsNot Nothing Then 
            Select Case HeaderCell.Text 
                Case "Molde" 
                    HeaderCell.ImageIndex = 0 
                    HeaderCell.Text = "" 
                Case "Print" 
                    HeaderCell.ImageIndex = 1 
                    HeaderCell.Text = "" 
                Case "EMail" 
                    HeaderCell.ImageIndex = 2 
                    HeaderCell.Text = "" 
                Case "Intranet" 
                    HeaderCell.ImageIndex = 3 
                    HeaderCell.Text = "" 
            End Select 
            HeaderCell.Alignment = ContentAlignment.MiddleCenter 
        End If 
    End Sub 

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

 
All the best,
Jack
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
GridView
Asked by
Xavier Soares
Top achievements
Rank 2
Answers by
Jack
Telerik team
Share this question
or