jknj72
asked on
Adding another attribute and checking value for DisplayName
This is practically the same question I posted last week about the Required Attribute but now I have another Attribute called DisplayName along with my RequiredAttribute and I want to check the Display Name I have set for it, via this attribute, and use that as my column header. I have a loop through my source data, check the attribute and set the header/column name. Can I do that in the RowDataBound like I did with the Required attribute? I tried adding another Where to the statement that was sent me but to no avail. Here is the code that fixed my Required attribute question and in bold is where I tried to add the DisplayName attribute, but its not working?
Protected Sub GridView_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim grid = DirectCast(sender, GridView)
Dim notRequired = (From [property] In e.Row.DataItem.GetType().GetProperties()
Where CType([property].GetCustomAttributes(GetType(RequiredAttribute), False), RequiredAttribute()).Count <= 0
[b] 'Tried adding this to check for DisplayName just like the line above it checks for Required attribute???[/b]
Where CType([property].GetCustomAttributes(GetType(DisplayAttribute), False), DisplayAttribute()).Count <= 0
Select e.Row.DataItem.GetType().GetProperties().ToList().IndexOf([property]))
For Each index In notRequired
If e.Row.Cells.Count - 1 > index Then
e.Row.Cells(index).Visible = False
End If
If grid.HeaderRow.Cells.Count - 1 > index Then
grid.HeaderRow.Cells(index).Visible = False
End If
Next
End If
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This works great and I apologize for not answering back sooner but I got pulled off this project. But what you gave me works great. I have another question regarding this so I hope you can take a look. Thanks again for your help
ASKER